0

What is the point of putting @Repository in service class CustomerServiceImpl and is it a good practice in this example, my understanding it is not needed for any interfaces that extend JpaRepository and exception translation already included. As far as I understand @Repository "should" be on repository class?

@Repository
@Transactional(readOnly = true)
public class CustomerServiceImpl implements CustomerService {

  @PersistenceContext
  private EntityManager em;

  @Autowired
  private CustomerRepository repository;
  ...
}



@Transactional(readOnly = true)
public interface CustomerRepository extends JpaRepository<Customer, Long> {}
2
  • Just quote from your given link The class is annotated with @Repository to enable exception translation from JPA exceptions to Spring’s DataAccessException hierarchy. and in article refactored the code in next portion to describe details of repository model Commented Aug 19, 2020 at 19:54
  • but isn't this already covered in CustomerRepository Commented Aug 19, 2020 at 19:55

1 Answer 1

1

Technically, the @Repository annotation may not be absolutely necessary in the given example for the CustomerServiceImpl class.

However, the use of @Repository annotation optimizes the 'search' and 'List all' operations by implementing the 'Domain Driven Design' pattern.

Example: The underlying repository can be a Relational database / No-sql database / a CSV file. Use of @Repository will hide the implementation complexity from 'Search' and 'List all' operations in this case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.