Hibernate is an excellent ORM framework embodied in:
1. Object-oriented design of the internal operation of the software can be understood to be in the continuous creation of new objects, the relationship between objects, call the method of the object to change the state of the object and the object of the process of extinction, regardless of the process and operation of the program, essentially to get a result, The difference in the running result of a time and the next moment in the program is a change in the state of the object in memory.
2. In order to maintain the running state of the program in the case of shutdown and insufficient memory space, it is necessary to save the state of the object in memory to the persistence device and restore the state of the object from the persistence device, usually saving the large amount of object information to the relational database. From the running function of Java program, the function of saving object state, compared with other functions of the system, should be a very humble subordinate function, Java uses JDBC to achieve this function, this humble function is to write a lot of code, and do is only to save objects and restore objects, And the large number of JDBC code does not have any technical content, basically is a set of routine standard code template to write, is a kind of hard work and repetitive work.
3. Save the object and restore object from the Java program runtime through the database, actually realize the mapping relation between Java object and relational database record, called ORM (that is, Object Relation Mapping), people can realize this function by encapsulating the JDBC code. The encapsulated product is called an ORM framework, and Hibernate is one of the popular ORM frameworks. Using the Hibernate framework, instead of writing the JDBC code, simply invoking a save method, you can save the object to a relational database, and simply call a GET method to load an object from the database.
4. The basic process for using Hibernate is to configure a configuration object, generate Sessionfactory, create a Session object, start a transaction, complete a crud operation, commit a transaction, and close the session.
5. When using Hibernate, To configure the Hibernate.cfg.xml file first, configure the database connection information and dialect, and configure the corresponding Hbm.xml file for each entity, you need to register each hbm.xml file in the Hibernate.cfg.xml file.
6. When applying hibernate, focus on understanding the session's caching principles, cascading, lazy loading, and hql queries.
First make sure that spring is integrated, in the previous blog, detailed how to integrate, and the three common ways
@Test publicvoid handle () { new Configuration (). Configure ("Hibernate.cfg.xml"); = configuration.buildsessionfactory (); = sessionfactory.opensession (); System.out.print (session); }
The above example is used to test if the integration is successful, and printing an address is successful.
Packagehibernate;Importjava.util.List;Importorg.hibernate.HibernateException;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;Importcom.tz.entity.Student; the @RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= "Classpath:config/applicationcontext.xml") Public classTestspringhibernate {@AutowiredPrivatesessionfactory sessionfactory;//Get sessionfactory @Test//Find Data Public voidHandle2 () {Session session=sessionfactory.opensession ();//Get session List<Student> students = session.createsqlquery ("SELECT * from Student"). Addentity (Student.class). List (); for(Student student:students) {System.out.println (Student.getname ()); System.out.println ("========="); }} @Test//Insert Data Public voidHandle3 () {Session session=sessionfactory.opensession (); Transaction TS=session.begintransaction ();//Open things Student Stu=NewStudent (); Stu.setname ("ZD"); Stu.setmale (1); Stu.setage (12); Try{session.save (stu); Ts.commit ();//Things submitted}Catch(hibernateexception e) {e.printstacktrace (); Ts.rollback ();//things rolled back}finally{session.close (); }} @Test ///delete data Public voidHandle4 () {Session session=sessionfactory.opensession (); Transaction TS=session.begintransaction (); Student St=NewStudent (); St.setid (1); Try{session.delete (ST); Ts.commit (); }Catch(Exception e) {e.printstacktrace (); Ts.rollback (); }finally{session.close (); }} @Test//Modify Data Public voidHandle5 () {Session session=sessionfactory.opensession (); Transaction TS=session.begintransaction (); Student St=NewStudent (); St.setid (2); St.setname ("Zengda"); Try{session.update (ST); Ts.commit (); }Catch(hibernateexception e) {e.printstacktrace (); Ts.rollback (); }finally{session.close (); } }}
Hibernate things simple additions and deletions to check and change