Core interfaces of Hibernate and core interfaces of hibernate

Source: Internet
Author: User

Core interfaces of Hibernate and core interfaces of hibernate

  1. Persistence and ORM

    Persistence refers to the storage of data (Objects in memory) to persistent storage devices (such as hard disks). It is mainly used to store data in memory to relational databases, in a three-tier structure, the persistence layer focuses on implementing the logic layer of the system and associates data users with data entities.

 

ORM is short for object-relational data ing. When writing a program, data is processed in an object-oriented manner, while data is saved in a relational database, therefore, a mechanism that can be converted between the two is required. This mechanism is called ORM. ORM stores the ing information between objects and relational database tables. The Hibernate ing information is stored in the XML format configuration file.

 

  2 Hibernate core interface Diagram

 

       2.1 Configuration class

When starting Hibernate, the Configuration instance first determines the hibernate ing file Hibernate. cfg. xml location, read the relevant configuration information, and finally create a unique SessionFactory instance, which is responsible for all persistent operations. Configuration instances only exist in the system initialization phase.

   

   2.2 SessionFactory Interface

SessionFactory interface is responsible for Hibernate initialization. Specifically, SessionFactory in Hibernate actually acts as a buffer zone. Hibernate automatically generates SQL statements, ing data, and some reusable data can all be placed in this buffer zone. It also saves all the ing relationships configured for the database and maintains the current second-level data cache and Statement Pool.

Generally, only one SessionFactory is required for a project. To operate multiple databases in a project, you must specify one SessionFactory for each database.

 

   2.2.1 main differences between openSession and getCurrentSession

When creating a Session instance, openSession is used, and SessionFactory creates a new Session instance. The Session instance created by the getCurrentSession method is bound to the current thread. The openSession method is used. After use, you need to call the close method to manually close it. The Session instance created by the getCurrentSession method is automatically closed when the commit or rollback operation is performed.

 

   2.3 Session Interface

The lifecycle of a Session object is bounded by the beginning and end of a Transaction object. Session provides a series of persistence-related operations, such as reading, creating, and deleting instances of Related Object objects. These operations will eventually be converted to the addition and modification of data in the database, query and delete operations. Therefore, Session is also called persistence manager.

 

   2.3.1 lifecycle of an object

The states of objects in the life cycle are: Transient, Persistent, and Detached ). After a single object performs different operations, its status is converted to each other.

(1) Instantaneous State

After the new operator is used to instantiate an object, the object is in the Instantaneous State. At this time, it is not stored in any database table. Once they are no longer referenced by other objects, their status will be lost immediately and cannot be accessed again. They can only wait for garbage collection.

(2) persistent state

The persistent State object is related to the Session instance. Any operations on the persistent State object will be persisted to the database by Hibernate. The records in the persistent State object sum Database correspond. When the Session cleans up the cache, It updates the database synchronously based on the changes in the persistent State object attributes. When the session executes the delete () method, it not only deletes the corresponding record from the database, but also deletes it from the session cache.

(3) Free State

When an object in the persistent state is no longer related to the Session instance, the object becomes free. In this case, the property value of the object changes, and Hibernate will not synchronize the changes to the database. For free-State objects, the Session executes the delete () method to delete the corresponding records from the database. The difference between the Free State and the instantaneous state is that the free state has a permanent state conversion. Therefore, the corresponding records are stored in the database, the instantaneous state does not have any records in the database and corresponds to it.

 

    2.3.2 how to enable an object to enter a persistent state

The update (), saveOrUpdate (), and lock () methods can convert free State objects to persistent State objects. Similar to the save () method, after the update () method is called, The saved object is not synchronized to the database immediately, the current attribute value of the object is updated to the database only when the transaction ends. However, to obtain accurate query results, the database needs to be updated in a timely manner. If a persistent State object is associated with an instantaneous state object, when cascade storage is allowed, the Session will convert the Instantaneous State object to a permanent State object during cache cleaning.

The saveOrupdate () method determines the method used by Hibernate Based on the object state. When the input object is in the instantaneous state, the save () operation is executed. When the input object is in the Free State, execute the update () method. When the input object is in the persistent state, it is returned directly without any operation.

Both the load () method and get () method use the identifier to the persistent State object of the specified class (type conversion is required), but the persistence object must exist. Otherwise, an exception occurs. The difference between the two is:

(1) When the queried record does not exist, the get () method returns null, while the load () method throws a HibernateException.

(2) The get () method returns the object class directly, and the load () method returns the proxy class instance of the object. Of course, if you set the lazy attribute of the <class> element in *. hbm. xml to false, the load () method also returns the object class without returning the proxy class. What is the use of the lazy attribute? It indicates delayed loading, that is, the real data loading operation is performed only when the attribute is really required. This can reduce the system performance overhead. When lazy () is set to true, load () returns the proxy instance of the Persistent object, which is dynamically generated at runtime, it includes all attributes and methods of the target object. However, due to delayed loading, all other attributes except the id attribute of the proxy instance are NULL. Only when the proxy instance calls the getXXX () method can Hibernate obtain the attribute values in the database.

(3) load () and get () both start from the internal cache of the Session to find the specified object. If not found, load () will go to the Hibernate second-level cache for query. If not found, an SQL statement is sent to the database for query and an object is generated based on the query results. Get () directly sends SQL statements to the database to read data beyond the second-level cache.

 

This article is all prepared by myself. If you think it is good, please pay attention to me or likes. As for Query, Criteria and Transaction interfaces, we will introduce them in the next Hql instance blog. Thank you for watching!

--------- Reference material: Java Web integrated development practices; Struts2 + Spring3 + Hibernate framework technical presentation and integration case

Related Article

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.