Precautions for integration of hibernate4 and spring3; otherwise, java. Lang. nosuchmethoderror is abnormal.

Source: Internet
Author: User

Recently, I have been studying the s2sh development framework. After reading the three parts separately, I started to integrate them. I found that the hibernate high version has improved many functions, leading to some discrepancies with the methods of integration with spring.

Let's talk about how to obtain the session when integrating with hibernate3.

For example, if the user name and password are correct, the data table is users, so myeclipse is used to generate the corresponding class and. HBM. the XML files are users. java and users. HBM. XML.

I directly added two frameworks using myeclipse, so the system automatically generates the applicationcontext. xml configuration file. The sessionfactory section is as follows:

 1       <  Bean  ID  = "Sessionfactory"  2   Class  = "Org. springframework. Orm. hibernate4.localsessionfactorybean"  > 3           <  Property  Name  = "Datasource"  >  4               <  Ref  Bean  = "Datasource"   />  5           </  Property  >  6          <  Property  Name  = "Hibernateproperties"  >  7               <  Props  >  8                   <  Prop  Key  = "Hibernate. dialect"  >  9  Org. hibernate. dialect. mysqldialect  10                   </  Prop  >  11               </  Props  >  12           </  Property  >  13           <  Property  Name = "Mappingresources"  >  14               <  List  >  15                   <  Value  > COM/ssh/entity/users. HBM. xml </  Value  >  16               </  List  > 17           </  Property  >  18       </  Bean  > 

In fact, here we can also see the integration of the two frameworks. The <property> tag of mappingresource is added to sessionfactory to implement or ing.
If you use hibernate3To inherit the hibernatedaosupport class in Dao, you can use:

1 Super.Gethibernatetemplate(cmd.exe cutefind (NewHibernatecallback (){2PublicObject doinhibernate (session)ThrowsHibernateexception, sqlexception {3 ......4 }5}

We can see that the parameter in doinhibernate () is session, so we can use it directly.
The key is, isn't it out of hibernate4?Many people have obsessive-compulsive disorder. They always like to use the premium version. Then, they use it and then follow the previous method. The result is an exception... I am one of my obsessive-compulsive patients... The exception is as follows:

 1  Exception in thread "Main" Java. Lang. nosuchmethoderror: org. hibernate. sessionfactory. opensession () lorg/hibernate/classic/session;  2   At org. springframework. Orm. hibernate3.sessionfactoryutils. dogetsession (sessionfactoryutils. Java: 324)  3   At org. springframework. Orm. hibernate3.sessionfactoryutils. getsession (sessionfactoryutils. Java: 235)  4   At org. springframework. Orm. hibernate3.hibernatetemplate. getsession (hibernatetemplate. Java: 457)  5   At org. springframework. Orm. hibernate3.hibernatetemplate. doexecute (hibernatetemplate. Java: 393) 6 At org.springframework.orm.hibernate3.hibernatetemplate.exe cutefind (hibernatetemplate. Java: 343)

One strange thing is that hibernate4 is clearly used. How is the exception information about hibernate3? Then I went back and looked at the inherited hibernatedaosupport, which is actually:

 
ImportOrg. springframework. Orm. hibernate3.support. hibernatedaosupport;

Later, I checked it online. The main reason is that hibernate4 already provides a good mechanism, so I don't need to inherit the hibernatedaosupport class. What should we do? Isn't it just to get the session? We directly add a sessionfactory attribute to the userdaoimpl. Java class, and then add its setter method. The Implementation Layer of the DaO layer is to be added to applicationcontext. XML, which is actually spring dependency injection. For example, add userdaoimol. Java in XML as follows:

  1     bean   id   =" userdao "  class   =" com. ssh. dao. userdaoimpl " >  2     property   name  =" sessionfactory "  ref  =" sessionfactory " >
     property  >  3     bean  > 

The sessionfactory mentioned above is automatically generated by the system. See hereCodeIt should be noted that here we have obtained the sessionfactory instance. As for its use, it must have been very familiar, not to mention. My userdaoimpl. Java code is as follows:

 1   Package  Com. Ssh. Dao;  2   3   Import  Java. util. List;  4   5   Import  Org. hibernate. criteria;  6   Import Org. hibernate. Session;  7   Import  Org. hibernate. sessionfactory;  8   Import  Org. hibernate. criterion. restrictions;  9   10   Import  Com. Ssh. entity. users;  11   12   Public   Class UserdaoimplImplements  Userdao {  13       Private  Sessionfactory;  14   15       Public   Void  Setsessionfactory (sessionfactory ){  16           This . Sessionfactory = Sessionfactory;  17   } 18   19       Public List Search ( Final  Users condition ){  20           //  Session session = sessionfactory. getcurrentsession ();  21 Session session = Sessionfactory. opensession ();  22 Criteria = session. createcriteria (users. Class  );  23          If (Condition! = Null  ){  24 String username = Condition. GetUserName ();  25 String Password = Condition. GetPassword ();  26               If (Username! = Null &&! Username. Equals ("" )){  27 Criteria. Add (restrictions. eq ("username", Username ));  28   }  29               If (Password! = Null &&! Password. Equals ("" )){  30 Criteria. Add (restrictions. eq ("password" , Password ));  31   }  32   }  33          Return  Criteria. List ();  34           35   }  36   37 }

Be sure to pay attention to the comments of Line 1 here. Do not use getcurrentsession (). Otherwise, an exception occurs. Use opensession () directly ()

 

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.