How does Hibernate work? Why use Hibernate?

(1) hibernate can be understood as a middleware. It is responsible for receiving the SQL statement of the Java program and sending it to the database , and the information returned by the database is received by Hibernate and directly generates an object to be passed to java.

There are two unique files in Hibernate, one is a mapping file ending with .hbm.xml, and the other is a configuration file ending with .cfg.xml. The function of the .cfg.xml file is to connect to the database. The inside of the file is actually the basic information of a link library composed of user, password, url, and driver. The .hbm.xml file is a mapping file for the tables in the database.

(2) How Hibernate works

①Read and parse the hibernate.cfg.xml configuration file;

②Read and parse the mapping information by <mapping resource="com/xx/user.hbm.xml"> in hibernate.cfg.xml;

③. Create SessionFactory through SessionFactory sf = config.buildSessionFactory();

④Session session = sf.openSession();//打开Sesssion

⑤Transaction tx = session.beginTransaction();//Create and start transaction Transaction

⑥persistent operate operation data, persistent operation

⑦tx.commit();//Submit the transaction

⑧Close Session

⑨Close SessionFactory

(3) The reasons for using Hibernate are as follows:

① Encapsulates the code for JDBC access to the database, which greatly simplifies the tedious and repetitive code of the data access layer.

       ②HIbernate is a mainstream persistence framework based on JDBC and an excellent implementation of object-relational mapping, which simplifies the coding work of the DAO layer to a great extent;

③Hibernate uses java reflection mechanism instead of bytecode enhancement program to achieve transparency;

       ④Hibernate performance is very good, because it is a lightweight framework, the flexibility of mapping is excellent, it supports various relational databases, from one-to-one to many-to-many complex relationships.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992922&siteId=291194637