The latest version of Hibernate is a professional open source object-relational mapping framework. The official version of Hibernate implements a very lightweight object encapsulation of JDBC, allowing Java programmers to freely use object programming thinking to manipulate the database. The latest version of Hibernate can be used in any situation where JDBC is used to complete the important task of data persistence.
Introduction to Hibernate software
There are 6 core interfaces in the Chinese version of Hibernate, namely: Session, SessionFactory, Transaction, Query, Criteria and Configuration. These 6 core interfaces will be used in any development. Through these interfaces, not only can persistent objects be accessed, but also transaction control can be performed.
Hibernate database design
a) Reduce the complexity of associations
b) Try not to use joint primary keys
c) ID generation mechanism, the mechanisms provided by different databases are not exactly the same
d) Appropriate redundant data, but not excessive pursuit of high paradigm
How Hibernate works
The working principle of Hibernate is that Configuration reads the information in Hibernate's configuration file and mapping file, that is, loads the configuration file and mapping file, and generates a multi-threaded SessionFactory object through the Hibernate configuration file.
Then, the multi-threaded SessionFactory object generates a threaded Session
Object; Session object generates Query object or Transaction object; it can be used through get(), load(), save(), update(), delete() and saveOrUpdate( of Session object
) and other methods to load, save, update, delete and other operations on PO.
In the case of query, you can use Session
The object generates a Query object, and then uses the Query object to perform query operations; if there are no exceptions, the Transaction object will submit the results of these operations to the database.
Hibernate caching mechanism
Level 1 cache: Hibernate's level 1 cache is provided by the session, so it only exists during the life cycle of the session. In other words, when the session is closed, it should
The first-level cache managed by the session is also cleared. Hibernate's first-level cache is built into the session. It is enabled by default and cannot be uninstalled or configured in any way. In slow
Objects in storage have persistence, and the session object is responsible for management. The advantage of the first-level cache is that you can use the same session object to query the same data object multiple times, and only query the database once. Level 1
The cache is implemented using the Key-Value MAP method. When caching entity objects, the primary key ID of the object is the Key of the MAP, and the entity object is the value of the object. So the first level cache is
It is stored in units of entity objects. The primary key keyword ID is used when accessing. The first-level cache uses the automatic maintenance function. However, you can use the manual method provided by the session to modify the first-level cache.
Management performs manual intervention. The evict() method is used to clear an object from the session's first-level cache. The clear() method is used to clear all methods in the session cache.
Hibernate usage instructions
1. The Session interface is responsible for performing CRUD operations on persisted objects (the task of CRUD is to complete communication with the database, including many common SQL statements.). But it should be noted that the Session object is not thread-safe. At the same time, Hibernate's session is different from HttpSession in JSP applications. When the term session is used here, it actually refers to the session in Hibernate, and the HttpSession object will be called the user session in the future.
2.SessionFactory interface is responsible for initializing Hibernate. It acts as a proxy for the data storage source and is responsible for creating Session objects. The factory pattern is used here. It should be noted that SessionFactory is not lightweight, because generally, a project usually only needs one SessionFactory. When multiple databases need to be operated, a SessionFactory can be specified for each database.
3. The Configuration class is responsible for configuring and starting Hibernate and creating SessionFactory objects. During the startup process of Hibernate, the instance of the Configuration class first locates the mapping document location, reads the configuration, and then creates the SessionFactory object.
4. The Transaction interface is responsible for transaction-related operations. It is optional, and developers can also design and write their own underlying transactions. 5. The Query and Criteria interfaces are responsible for executing various database queries. It can be expressed in two ways: HQL statement or SQL statement.
Hibernate update log
Working day and night just so you can be happier
are all gone!
Huajun editor recommends:
Don't miss it when you pass by. If you miss Hibernate, you will never experience such a good service again. This site also recommends to you Tarma QuickInstall, cc3d parameter adjustment software OpenPilot GCS, and Batch Butler