Hibernate integration and spring Integration
Hibernate is an excellent persistence framework that simplifies the process of saving objects to the database, or reading data from the database and encapsulating the data into objects. Hibernate uses simple configuration and encoding to replace the tedious jdbc program code.
The following describes how to integrate hibernate.
Download the hibernate. jar package and reference it in the project.
<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> Create and compile object class configuration files
<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<Generator class = "/> Configure the primary key generation policy. The values are as follows:
Increment: For Primary keys of the long, short, and int types, the value of the primary key is generated in the mode of automatic growth. The increment is 1.
Sequence: for databases that support sequences, sequence can be used to assign values to the primary key. param sub-elements are used to specify the sequence name.
Identity: this policy can be used for databases that support identity columns. However, you must set this column as an ID column in Data Control.
Native: hibernate automatically determines which policy to use
Assigned: The primary key is generated by an application. hibernate does not need to participate.
After compiling the object configuration file, add reference to the configuration file in the hibernate. cfg. xml file.
Configuration cfgt = new Configuration (). configure (); // create the configuration object to hibernate. cfg. the information in the xml file is encapsulated in the cfgt object SessionFactory = cfgt. buildSessionFactory (); // create the command factory Session = factory. openSession (); // open the command Object Transaction tsc = session. beginTransaction (); // starts the transaction session. save (stu); save the information in stu to the database (stu is the object class object created) tsc. commit (); // submit the transaction session. close (); // close the command object