How Hibernate works

Source: Internet
Author: User

How Hibernate works
Initialization of the 1.Hibernate.
Read Hibernate configuration information-〉 Create session Factory
1) Create an instance of the Configeration class.
How it is constructed: reads the configuration information (Hibernate config) into memory.
A Configeration instance represents the collection of Hibernate all Java classes to SQL database mappings.
2) Create a Sessionfactory instance
Copies all configuration information from the Configeration object to the Sessionfactory cache.
An instance of Sessionfactory represents a database storage-clerk source that is no longer associated with the Configeration object after it is created.
Cache: Refers to the properties of a Java object, which is typically a property of some collection type-consuming memory space.
Sessionfactory in cache: Hibernate configuration information. or mapping metadata.
Cache-Large: Heavyweight objects small: Lightweight objects
3) Call Sessionfactory method to create session
1 "The user provides the JDBC connection themselves.
Connection con=datasource.getconnection ();
Session s=sessionfactory.opensession (con);
2 "Let Sessionfactory provide connection
Session s=sessionfactory.opensession ();
4) Manipulate database access through a variety of methods provided by the session interface.
Hibernate's caching system
First-level caching:
The Session has a built-in cache that holds objects loaded by the current unit of work.
Each session has its own independent cache and can only be accessed by the current unit of work.
Second-level cache:
Sessionfactory's external pluggable cache plug-in. The data can be accessed by multiple session sharing.
Sessionfactory's built-in cache: Store mapped metadata, predefined SQL statements.
Status of Java objects in Hibernate
1. Temporary status (transient)
Characteristics:
1 "is not in the session cache
2 "There are no object records in the database
How Java enters a temporary state
1 "When you create an object by using the new statement
2 "When the Delete () method of the session is called, when an object is deleted from the session cache.
2. Persistent state (persisted)
Characteristics:
1 "in session cache
2 "Object records are available in the persistent object database
3 "session will stay synchronized at a specific time
How Java enters the persistence state
1 "Session Save () put temporary-" persistent state
2 "the load () of the Session, the object returned by the Get () method
3 "The Find () of the Session returns the object stored in the list collection
4 "Session update (), saveorupdate () Make free-" persistent
3. Free State (detached)
Characteristics:
1 "is no longer in the session cache
2 "free objects are transformed from a persistent state, and there may be records in the database.
How Java enters the persistence state-"Free State"
1 "The Close () method of the Session
2 "Session of the Evict () method to remove an object from the cache. Improve performance. Use less.
I studied Hibernate for two days, looked at a lot of information, and read the key categories, and felt that I had a lot of places to study. For example, how Hibernate's caching mechanism is implemented, what hibernate does in encapsulating JDBC and JTA, how its ORM is implemented, and so on. To find out that these things are estimated to take a long time to explore and accumulate, but also in practical applications continue to deepen their understanding of:) below, I will summarize the last two days of the harvest, mainly about hibernate basic work principle. For hibernate cache mechanism, JDBC and JTA-based transaction processing and implementation of the ORM, and so on, slowly re-fill it later.
The basic working principle of hibernate, in other words, is the general work flow of hibernate. Here, we modeled the previous approach to the fundamentals of struts, only the schematic diagram presented to you, detailed text description in this skip.

Overview:

1. After configuring Hibernate's configuration file and the configuration file corresponding to the class, start the server
2. The server instantiates the Configeration object, reads the configuration contents of the Hibernate.cfg.xml file, and builds the table or establishes a mapping relationship with the table according to relevant requirements.
3. The Sessionfactory instance can be established by instantiating the Configeration object, further, the session object can be created by Sessionfactory instance
4. After the session, the database can be added to check the operation, in addition to more complex full-text search, simple operation can be done through the Hibernate package session built-in method to achieve
5. In addition, it is possible to achieve more complex database design through the association of Things management and tables.
Advantage: Hibernate corresponds to the bridge between Java class and database table, we can do a lot of things through this bridge.

How Hibernate works

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.