Reference book: "Java EE open source programming Essentials 15"
Hibernate is the solution for object/relational mapping (Orm,object/relational Mapping), which is to map Java objects and object relationships to the relationships between tables and tables in a relational database. It is a bridge between Java applications and relational databases and is responsible for mapping between Java objects and relational data. Hibernate internally encapsulates the operation of accessing the database through JDBC, providing an object-oriented database access API to upper-level applications.
The architecture of Hibernate
Hibernate uses a configuration file (Hibernate.properties or Hibernate.cfg.xml) and a mapping file (. hbm.xml) To put Java objects or persisted objects (persistent object,po) Map to the database table, and then by manipulating the PO, the data in the table to increase, delete, change, check and other operations.
Hibernate's core components
- Configuration class: Used to read Hibernate configuration files and generate Sessionfactory objects.
- Sessionfactory interface: The factory that generated the session instance
- Session interface: Used to operate the PO. A get (), load (), save (), update (), delete (), and other methods used to load, save, update, and delete the PO are the core interface of Hibernate
- Query interface: Used for querying the PO. Can be generated from the CreateQuery () method of the session
- Transaction interface: Used to manage hibernate transactions, the Main method is commit () and rollback (), can be generated from the session's BeginTransaction () method
- Hibernate profiles are primarily used to configure database link parameters, such as database drivers, URLs, user names, passwords, and so on. There are two types of formats: Hibernate.properties and Hibernate.cfg.xml,hibernate.cfg.xml can be used in their <mapping> The Xxx.hbm.xml Map file list is defined in the child element, and hibernamte.properties needs to be specified in the program as hard code. The general Hibernate.cfg.xml is Hibernate's default configuration file
- The mapping file (xxx.hbm.xml) is used to map the relationship between the PO and the data tables in the database, the relationship between the PO and the database, and the properties of the PO with the table field one by one, which is the core file of hibernate.
- Persistent Objects (PO) can be normal Javabean/pojo, and the only special thing is that they are associated with (just one) session. JavaBean has three states in Hibernate: temporal State (transient), persistent state (persistent), and managed state (detached). A temporary object when an JavaBean object is orphaned in memory, is not associated with data in the database, is persisted when it is associated with a session, and when the session is closed, the object becomes a de-tube object.
Hibernate's Running Process
1) The application calls the configuration class, which reads the information from the Hibernate profile-level mapping file and generates a sessionfactory
2) Generate a Session object from the Sessionfactory object and generate the transaction object with the Session object
3) loading, saving, updating and deleting po by means of get (), load (), save (), update (), delete (), saveorupdate (), etc. of the Session object ; Generate a query object from the session object for querying operations
4) If there is no exception, the transaction object will submit the results of these operations to the database
Application example Simple user registration: Http://pan.baidu.com/s/1dDGSzQh
Related information: http://yusun.blog.163.com/blog/static/20174407620120432015398/
Introduction to the main components of Java Web programming technology--hibernate