What is your question actually about? The usage of the @Repository
annotation or @Transactional
.
@Repository
is not needed at all as the interface you declare will be backed by a proxy the Spring Data infrastructure creates and activates exception translation for anyway. So using this annotation on a Spring Data repository interface does not have any effect at all.
@Transactional
– for the JPA module we have this annotation on the implementation class backing the proxy (SimpleJpaRepository
). This is for two reasons: first, persisting and deleting objects requires a transaction in JPA. Thus we need to make sure a transaction is running, which we do by having the method annotated with @Transactional
.
Reading methods like findAll()
and findOne(…)
are using @Transactional(readOnly = true)
which is not strictly necessary but triggers a few optimizations in the transaction infrastructure (setting the FlushMode
to MANUAL
to let persistence providers potentially skip dirty checks when closing the EntityManager
). Beyond that the flag is set on the JDBC Connection as well which causes further optimizations on that level.
Depending on what database you use it can omit table locks or even reject write operations you might trigger accidentally. Thus we recommend using @Transactional(readOnly = true)
for query methods as well which you can easily achieve adding that annotation to you repository interface. Make sure you add a plain @Transactional
to the manipulating methods you might have declared or re-decorated in that interface.