Spring cannot inject value into Sessionfactory, and org.hibernate.HibernateException:createQuery is not valid without active Transaction exception

Source: Internet
Author: User

in the framework of STRUTS2+SPRING3+HIBERNATE4, originally wanted to inject in DAO through setter, assign value to Sessionfactory, but appear nullpointerexception exception. After debugging, the value of sessionfactory is found to be null. It turns out that in the service layer, the DAO class object is derived from new, not through spring. Therefore, the new DAO object, which is not injected by spring into the sessionfactory, is bound to be sessionfactory empty. Instead, the object is supplied by spring and the problem is successfully resolved.

In fact, one of the meaning and benefits of spring is to save the project from the new "abyss", using spring to provide the object uniformly by dependency injection, in order to achieve low coupling (which can be achieved with Factory mode if not spring). But the Factory mode object still needs new, so it doesn't have the same low coupling as spring's dependency injection. But I'm using new to create objects here, which is a huge violation of spring's main thrust. I wrote this code a long time ago, and it seems that I didn't understand spring well enough.

Another problem is that in a DAO file, the session is obtained by Sessionfactory.getcurrentsession (), which reports the exception: Org.hibernate.HibernateException:createQuery is not Valid without active transaction. After the experiment, it was found that <property name= "Current_session_context_class" >thread</property> attribute in Hibernate configuration file was removed. The original "Current_session_context_class" attribute means that the context of the current session is set, and if it is set to thread, the same session is shared by the same thread. Thus the session obtained through Getcurrentsession () is the session on the same thread, not the one that the spring manages to automatically open the transaction. It would be nice to get rid of the attribute.

Found in some books and on the Internet, if you want to use Getcurrentsession (), you must add <property name= "Current_session_context_class" in Hibernate configuration file > Thread</property> this property. I did what they said, but it turned out to be a problem. It seems that the letter is not as good as the book, others agree that the conclusion is not necessarily correct, in some cases may be wrong.

Spring cannot inject values into sessionfactory, and org.hibernate.HibernateException:createQuery is not valid without active Transaction exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.