Zero, About Hibernate
Hibernate means hibernation, which refers to the hibernation of animals, but the Hibernate discussed in this article has nothing to do with hibernation, but is a member of the SSH2 framework to be discussed next. Hibernate is an open source project. It is a framework of object-relational models and has a very lightweight encapsulation of JDBC. Programmers can use object programming thinking to develop during development.
Download address: http://hibernate.org/orm/downloads/
Note: The difference between lightweight and heavyweight. The lightweight framework package is smaller and simple to use, and is easy to test and has high development efficiency. The heavyweight framework package is larger, and the business process of internal packaging is more complicated and difficult to test, such as Struts.
Object relational model:
Hibernate implements the mapping of object-relational models. When programming, programmers can directly use the object model to operate the database. It lightweight encapsulates JDBC and also encapsulates SQL statements for database operations, which are simple to use. Although it has many advantages, it will be difficult to tune using statements with database characteristics, such as stored procedures, etc.
Pros and cons of Hibernate:
(1) Advantages
A. Improve productivity;
B. Make development more objectified (impedance mismatch);
C. Portability;
D. No invasiveness and supports transparency and persistence.
(2) Disadvantages
A. Statements using database characteristics will be difficult to tune;
B. There are problems with large-scale data updates;
C. There are a large number of statistical query functions in the system.
2. Hibernate example
The above article provides some preliminary interpretations of Hibernate. With theory, there is certainly more practice. You don’t understand its convenience without using Hibernate. This is just like a person who likes to drink tastes Moutai for the first time. Only after using it can you understand it more deeply.
The following example uses the MySQL database. A database named Hibernate_first is created in MySQL. A User table is created through the Hibernate mapping file using object programming method and add information to the User table.
Specific steps:
(1) Create a normal Java Application;
(2) Add the jar package of Hibernate. When adding the jar package, you need to introduce Hibernate.jar, third-party jar packages referenced by Hibernate, and jar packages connected to Hibernate and mysql.
(3) Add the database connection configuration file Hibernate.cfg.xml.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">ab12</property> <!-- dialect:Dialect, the encapsulated underlying API, similar to Runtime, converts the database into the corresponding language in the configuration --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Set the data display operation on the database --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="com/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
(4) Create the entity class name as User.java
package com.hibernate; import java.util.Date; public class User { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getExpireTime() { return expireTime; } public void setExpireTime(Date expireTime) { this.expireTime = expireTime; } private String name; private String password; private Date createTime; private Date expireTime; } (5) Create the mapping file User.hbm.xml of the User entity class, complete the mapping of the entity class, and add the file to the Hibernate.cfg.xml file.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2014-4-30 15:39:33 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.hibernate.User"> <id name="id"> <generator/> </id> <property name="name"/> <property name="password"/> <property name="createTime"/> <property name="expireTime"/> </class> </hibernate-mapping>
(6) Write ExportDB.java and convert the mapping file into the corresponding DDL.
package com.hibernate; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB { public static void main(String[] args){ //First declare to get the configuration file//Default read Hibernate.cfg.xml file Configuration cfg=new Configuration().configure(); //Export the read xml file to ddl SchemaExport export=new SchemaExport(cfg); export.create(true, true); } } Run the ExportDB class to complete the creation of the database table. The view after viewing the specific operation in cmd is as follows:
The above example only completes the operation of connecting to the database and creating tables in the database. After creating the table, you need to add data to the table, create a client class client, and add new user information to the User table. The specific code is as follows:
package com.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args){ //Read hibernate.cfg.xml file Configuration cfg=new Configuration().configure(); //Create sessionfactory, which is equivalent to a database mirror. Because sessionfactory is a mirror, it is best to create it once/usually thread-safe. SessionFactory factory=cfg.buildSessionFactory(); //Fetched session Session session=null; try{ session=factory.openSession(); //Open transaction session.beginTransaction(); User user=new User(); user.setName("Zhang San"); user.setPassword("123"); user.setCreateTime(new Date()); //Save User object session.save(user); //Commit transaction session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace();//Print error message//Rollback transaction session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //Close session session.close(); } } } } } } } } } View added information in mysql as shown in the following figure:
The information of the above operation has been written to the database. After saving the data in the database, the corresponding rows are generated in the database. However, it has not been saved yet. Instead, there is corresponding rows of data in the database. The data is submitted to the database after the transaction submission using session is completed.