Service class for the entity

Since we opted to generate service classes for our entities, let's look at one. In the src/main/java/com/mycompany/store/service folder, you will find the entity repository service. Open ProductService.java:

@Service
@Transactional
public class ProductService {

private final Logger log = LoggerFactory.getLogger(ProductService.class);

private final ProductRepository productRepository;

public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}

...
}

The service uses constructor injection to get its dependencies, which are automatically injected by Spring during bean instantiation. The service is also marked as @Transactional to enable transaction management for data access. The service defines CRUD action methods—for example, the findAll method calls the equivalent repository method while adding a read-only transaction rule to it. You can see that the method already supports pagination and returns the results as Page. The Page and Pageable objects are provided by Spring and let us easily control pagination:

    @Transactional(readOnly = true)
public Page<Product> findAll(Pageable pageable) {
log.debug("Request to get all Products");
return productRepository.findAll(pageable);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.89