Getting started with the Hibernate framework
1. Hibernate framework
Core Components of Hibernate
In java web applications based on the MVC design pattern, Hibernate can be used as the model layer/data access layer. It uses the configuration file (hibernate. properties or hibernate. cfg. xml) and ing files (***. hbm. xml) maps a JAVA Object or PO (Persistent Object, persistence Object) to a database, and then adds, deletes, modifies, and queries data in the data table through the PO operation.
In addition to the configuration file, ing file, and persistence class, the core components of Hibernate include the following:
A) Configuration class: used to read the Hibernate Configuration file and generate the SessionFactory object.
B) SessionFactory interface: generate the Session instance factory.
C) Session interface: used to operate PO. It has methods such as get (), load (), save (), update (), and delete () to load, save, update, and delete PO. It is the core interface of Hibernate.
D) Query interface: Used to Query PO. It can be generated from the createQuery () method of the Session.
E) Transaction interface: used to manage Hibernate transactions. The main methods include commit () and rollback (), which can be generated from the beginTrancation () method of the Session.
Persistent Object
Persistence objects can be common Javabeans. The only special difference is that they are associated with (only one) Session. JavaBeans has three statuses in Hibernate:
1. temporary State (transient): When a JavaBean Object exists in the memory and does not have any association with the data in the database, the JavaBeans Object is called a temporary Object ).
2. persistent: When a JavaBean Object is associated with a Session, it becomes a Persistent Object)
3. detached: When this Session is closed, the Object will also be out of the persistent state and become a Detached Object ), it can be freely used by any layer of the application, for example, a Data Transfer Object that can deal with the presentation layer ).
Hibernate running process
Hibernate runs as follows:
A: The application first calls the Configration class, which reads information in the Hibernate configuration file and ing file, and generates A SessionFactpry object with this information.
B: generate a Session object from the SessionFactory object and use the Session object to generate a Transaction object. You can use the get (), load (), save (), update (), delete () and saveOrUpdate () methods are used to load, save, update, and delete PO. When querying, a Query object can be generated through the Session object, then the Query object is used to perform the Query operation. If no exception occurs, the Transaction object submits the operation results to the database.
The running process of Hibernate is as follows:
2. Entry case
01. Prepare various jar packages (I don't need to teach it)
02. Prepare the Student Entity class (used to operate the corresponding database)
Package cn. zhang. entity; // entity class public class Student {private int stuno; private String stuname; private int stuage; private int stuid; private int stuseat; public Student () {super (); // TODO Auto-generated constructor stub} public Student (String stuname, int stuage, int stuid, int stuseat) {super (); this. stuname = stuname; this. stuage = stuage; this. stuid = stuid; this. stuseat = stuseat;} public Student (int stuno, String stuname, int stuage, int stuid, int stuseat) {super (); this. stuno = stuno; this. stuname = stuname; this. stuage = stuage; this. stuid = stuid; this. stuseat = stuseat;} public int getStuid () {return stuid;} public void setStuid (int stuid) {this. stuid = stuid;} public int getStuseat () {return stuseat;} public void setStuseat (int stuseat) {this. stuseat = stuseat;} public int getStuno () {return stuno;} public void setStuno (int stuno) {this. stuno = stuno;} public String getStuname () {return stuname;} public void setStuname (String stuname) {this. stuname = stuname;} public int getStuage () {return stuage;} public void setStuage (int stuage) {this. stuage = stuage ;}}
03. design the Hibernate configuration file hibernate. cfg. xml under src
<? 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 "> <Hibernate-configuration> <session-factory> <! -- Database connection settings --> <property name = "connection. driver_class "> oracle. jdbc. oracleDriver </property> <property name = "connection. url "> jdbc: oracle: thin: @ localhost: 1521: orcl </property> <property name =" connection. username "> zhangzong </property> <property name =" connection. password "> 123 </property> <! -- SQL dialect (SQL dialect) --> <property name = "dialect"> org. hibernate. dialect. Oracle10gDialect </property> <! -- Drop and re-create the database schema on startup --> <property name = "hbm2ddl. auto"> update </property> <! -- Echo all executed SQL to stdout: print the background SQL statement on the console --> <property name = "show_ SQL"> true </property> <! -- Format and display SQL --> <property name = "format_ SQL"> true </property> <! -- JDBC connection pool (use the built-in) --> <! -- <Property name = "connection. pool_size"> 1 </property> --> <! -- Enable Hibernate's automatic session context management --> <! -- <Property name = "current_session_context_class"> thread </property> --> <! -- Disable the second-level cache --> <! -- <Property name = "cache. provider_class "> org. hibernate. cache. noCacheProvider </property> --> <mapping resource = "cn/zhang/entity/Student. hbm. xml "/> </session-factory>
04. design the stuing file Student. hbm. xml in the object class (used in the configuration file hibernate. cfg. xml)
<? 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">
05. Add a test class
001. Update (new) one student record
Package cn. zhang. test; // Add a new data import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. hibernate. classic. session; import cn. zhang. entity. student; public class InsertTest {public static void main (String [] args) {// prepare the object Student student = new Student ("Guangyi", 2 ); // Student. hbm. the xml has been configured with an auto-increment number, so no serial number is added here. // read the large Configuration file and obtain the information of the database to be connected. configuration Configuration = new Configuration (). configure (); // create SessionFactory factory = configuration. buildSessionFactory (); // process session Session openSession = factory. openSession (); Transaction beginTransaction = openSession. beginTransaction (); openSession. save (student); beginTransaction. commit (); System. out. println ("successful ");}}
002. modify a student information
Package cn. zhang. test; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. hibernate. classic. session; import cn. zhang. entity. student; public class UpdateTest {/*** @ param args */public static void main (String [] args) {// 1. read the large Configuration file and obtain the information of the database to be connected, Configuration conf = new Configuration (). configure (); // 2. create SessionFactory factory = conf. buildSessionFactory (); // 3 process session Session session = factory. openSession (); Transaction tx = session. beginTransaction (); // get the object Student stu = new Student (1, "Guangyi", 2); // update the session. update (stu); // submit the transaction tx. commit (); System. out. println ("updated successfully ");}}
003. delete a specified Student Information
Package cn. zhang. test; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. cfg. configuration; import org. hibernate. classic. session; import cn. zhang. entity. student; public class DeleteTest {public static void main (String [] args) {// 1. read the large Configuration file and obtain the information of the database to be connected, Configuration conf = new Configuration (). configure (); // 2. create SessionFactory factory = conf. buildSessionFactory (); // 3. processing session Session session = factory. openSession (); Transaction tx = session. beginTransaction (); // get the object Student stu = new Student (); stu. setStuno (3); // specify the number to be deleted // Delete the specified session. delete (stu); // submit the transaction tx. commit (); System. out. println ("deleted successfully ");}}