How to fix "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional" exception
If there are two application context files, for example in case of spring web application, one is servlet-context and other is application-context, then mostly this exception comes.
The reason for of this exception is that when spring starts loading and initializing the beans it first looks into servlet-context, and loads all beans and its dependencies. All controllers and its dependency (e.g. all @Autowired properites) are also loaded and initialized.
Since most of the time we don't configure datasources and hibernate related configuration in servlet-context, so while loading the servlet-conext spring dose not created and loads hibernate sessions and configurations. Solution: in servlet-context, only write the package which has your controller classes e.g.
in this case this will not try to load service classes with @Service annotation and those will be loaded while loading the application-context and at that time hibernate configuration will also be available to inject into @Service classes. comments powered by Disqus