Understanding Spring and “No EntityManager with actual transaction available”

南笙酒味 提交于 2020-01-05 16:28:58

问题


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?

来源:https://stackoverflow.com/questions/34948383/understanding-spring-and-no-entitymanager-with-actual-transaction-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!