Reputation: 6532
I just saw two different projects.
In one of them whenever you tried to persist/save an entity from a method with no @Transactional
annotation there was an exception that said: No session found
On the other
Even though no @Transactional
annotation exists the save
method allows saving.
PS - One project uses Spring + Hibernate (with session not found
exception), the other uses Spring + JPA + Spring Data repositories (allows saving with no transactional annotation)
Any ideas why the differences? What's the best practice?
Upvotes: 0
Views: 114
Reputation: 691755
That's because, as the documentation of Spring Data JPA indicates
CRUD methods on repository instances are transactional by default. For reading operations the transaction configuration readOnly flag is set to true, all others are configured with a plain @Transactional so that default transaction configuration applies. For details see JavaDoc of Repository.
Upvotes: 1