Hibernate's Core interface

Source: Internet
Author: User

  1. Persistence and ORM

    Persistence refers to storing data (in-memory objects) in a durable storage device (such as a hard disk), which is primarily used to store in-memory data in a relational database, and in a three-tier structure, the persistence layer focuses on implementing the logical plane of the system, associating the data consumer with the data entity.

ORM abbreviation Object-relational data mapping, when writing programs, processing data in an object-oriented manner, the preservation of data in a relational database, it is necessary to be able to convert between the two mechanisms. This mechanism is called ORM. ORM holds mapping information for objects and relational database tables. Hibernate mapping information is saved in an XML-formatted configuration file.

  2 Hibernate Core Interface block diagram

    

   2.1 Configuration Class

In the process of starting hibernate, The configuration instance first determines the location of the mapping file Hibernate.cfg.xml, reads the relevant configuration information, and finally creates a unique sessionfactory instance, which is responsible for all persistent operations of the Sessionfactory instance. An instance of the configuration class exists only at the initialization stage of the system.

   

   2.2 Sessionfactory Interface

The Sessionfactory interface is responsible for the initialization of hibernate. Specifically, sessionfactory actually acts as a buffer in Hibernate , and Hibernate automatically generates SQL statements, mapped data, and some reusable data that can be placed in this buffer. It also preserves all mapping relationships to the database configuration, maintaining the current level two data cache and statement Pool.

In general, a project requires only one sessionfactory, but when you are working with multiple databases in a project, you must specify a sessionfactory for each database.

   The main differences between 2.2.1 Opensession method and Getcurrentsession method

When you create a session instance, a new session instance is created with Opensession,sessionfactory. The session instance created by the Getcurrentsession method is bound to the current thread . With the Opensession method, you need to show the Close method to manually close after you have finished using it. The session instances created by the Getcurrentsession method are automatically closed when committing or rolling back (rollback) operations.

   2.3 Session Interface

The life cycle of a Session object is bounded by the start and end of things that transaction objects. The session provides a series of actions related to persistence, such as reading, creating and deleting instances of related entity objects, and this series of operations will eventually be converted to add, modify, query, and delete operations to the data in the database. Therefore, the session is also known as the Persistence manager .

   2.3.1 The life cycle of an entity object

The state of an object in its life cycle is transient (Transient), persistent (persistent), and free (Detached). The state of the same entity object is converted to each other after different operations.

(1) Instantaneous state

The state of the object that is instantiated with the new operator is instantaneous, and is not stored in any database tables. Once they are no longer referenced by other objects, the state is immediately lost and can no longer be accessed and can only wait for garbage collection.

(2) Persistent state

The persisted object is related to the session instance, and any action on the persisted object is persisted to the database by hibernate. The persistent state object sums up the records in the database, and when the session caches the cache , the database is updated synchronously, depending on the properties of the persisted object . When the session executes the delete () method, not only will the corresponding record be deleted from the database, but it will also be removed from the session's cache.

(3) Free State

When an object that is in a persistent state is no longer associated with the session instance, the object becomes a free state. When the property values in the object change, hibernate no longer synchronizes the changes to the database . For an object with a free state, the session Execution Delete () method deletes the corresponding record from the database. The difference between the Free State and the instantaneous state is that the Free State is transformed by a persistent state, so there is a corresponding record in the database, and the instantaneous state does not exist any records in the database and it corresponds.

    2.3.2 the way an object enters a persistent state

The update (), Saveorupdate (), and Lock () methods can convert a free-state object to a persistent state object. Similar to the Save () method, the saved object is not synchronized to the database immediately after the call to the update () method, and the current property value of the entity object is updated to the database only when the thing is finished, but when a new query of the same table needs to be executed, You need to update the database in a timely manner. If a persistent state object is associated with a transient state object, the session converts the transient object to a persisted object when the cache is cleared, in case the cascade is allowed to save .

The Saveorupdate () method determines what method is used by hibernate depending on the state of the entity object, performs the Save () operation when the incoming entity object is transient, executes the update () method when the incoming object is free, and returns directly when the passed-in is a persistent state. No action is made.

The load () method, or the Get () method, is a persistent object of the specified class through the identifier (requires a strong type), but requires that the persisted object be present, otherwise an exception is generated. The difference between the two is:

(1) When the found record does not exist, the Get () method returns NULL, and the load () method throws a Hibernateexception exception.

(2) The Get () method directly returns , the Load () method returns The proxy class instance of the entity . Of course, if you set the Lazy property of the <class> element in *.hbm.xml to False, the load () method also returns the entity class and does not return the proxy class.      What is the use of the lazy attribute, he said lazy loading, that is, when a property is really needed, perform a true data load operation , which can reduce the performance overhead of the system. When lazy () is set to True, load () returns the proxy class instance of the persisted object, which is dynamically generated at run time and includes all the properties and methods of the target object. However, due to deferred loading, the properties of this proxy instance are null in addition to the id attribute, and hibernate actually obtains the attribute value in the database only when the proxy class instance calls the GetXXX () method.

(3) load () and get () are all starting from the session internal cache to find the specified object, if not found, load () will go to hibernate level two cache query, if not found, will send an SQL statement to the database for query, and generates the corresponding entity object based on the query results. The get () passes through the level two cache, issuing a SQL statement directly to the database to complete the data read.

    This article all for their own word finishing get, if you feel good , please pay attention to me or praise, as for the Query,criteria and transaction interface will be in the next hql examples of the blog to introduce, thank you to watch!

---------Reference "Java Web Integration Development Combat" "struts2+spring3+hibernate framework technology explaining and integration case"

Hibernate's Core interface

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.