I have a Spring Boot application with Hibernate, with a bunch of DAOs annotated with @Repository
and Spring's @Transactional
. Everything works fine. Then I decide to move common methods (persist, find, merge) to an AbstractDao
. Writing operations (persist, merge) start throwing that No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
exception. I try adding @Transactional
to AbstractDao
, and contrary to my expectations, it fixes it.
Why is that so? I thought that since, unlike CGLIB, Spring uses interfaces instead of extending classes, it wouldn't work, and I would need to define an interface to declare instead of my DAOs. But I also guess that I'm mixing concepts of how Spring deals with dependency management and with transactions.
Can somebody provide an explanation of why does it work? Is there a better way of dealing with this problem?