Get (), load (), and Hibernate in Hibernate

Source: Internet
Author: User

Get (), load (), and hibernate in hibernate

The session first checks whether the object exists. If the object exists, it cannot be retrieved in the database tutorial. The returned objects are all in the session cache. After modifying the attributes of the persistence object, when the session clears the cache, the database will be updated synchronously Based on the attribute changes of the persistence object.

Differences:

(1) When there is no record corresponding to the oid in the database, the load () method throws the objectnotfoundexception exception, and the get () method returns null.

(2) The two adopt different search strategies.

By default, the load () method uses a latency retrieval Policy (hibernate does not execute the select statement and only returns the proxy class instance of the object class, occupying a small amount of memory); While get () adopt an immediate search policy (hibernate will immediately execute the select statement ).

Usage:

(1) If you load an object to access its attributes, you can use get ();

(2) If the purpose of loading an object is to delete it or establish an association with another object, you can use load ();


Differences between get/load loading for hibernate data
The session. load/get method can read records from the database based on the specified object class and id, and return the corresponding object. The difference is:

If no matching record is found, the get method returns null, and the load method throws an objectnotfoundexception.
The load method returns the proxy class instance of the object, while the get method returns the object class directly.
The load method can make full use of the existing data in the internal cache and the second-level cache, while the get method only searches for data in the internal cache. If no corresponding data is found, the second-level cache will be crossed, directly call SQL to read data.
When a session loads an object, it goes through the following process:

First, two levels of cache are maintained in hibernate. The first level of cache is maintained by the session instance, which maintains the data of all associated entities of the session, also known as the internal cache. The second-level cache exists at the sessionfactory level and is shared by all session instances constructed by the current sessionfactory. In order to avoid unnecessary database access for performance consideration, session queries in the cache before calling the database query function. First, in the first-level cache, the object type and id are used for search. If the first-level cache query hits and the data status is valid, the system returns the result directly.
Then, the session will be searched in the current "nonexists" record. If the "nonexists" record has the same query condition, null is returned. "Nonexists" records the query conditions of the current session instance in all previous query operations, and the query conditions of valid data cannot be queried (equivalent to a query blacklist list ). In this way, if an invalid query condition reoccurs in the session, you can quickly make a judgment to obtain the best performance.
For the load method, if no valid data is found in the internal cache, the second-level cache is queried. If the second-level cache hits, the second-level cache returns.
If no valid data is found in the cache, the database query operation (select SQL) is initiated. If no corresponding record is found after the query, the query information is recorded in "nonexists, and return null.
Create the corresponding data object based on the resultset obtained by the ing configuration and select SQL.
Include the data objects in the current session Object Management container (level-1 cache ).
Execute the interceptor. onload method (if there is a corresponding interceptor ).
Include data objects in the second-level cache.
If the data object implements the lifecycle interface, the onload method of the data object is called.
Return Data Object


Brief Introduction to the functions of several packages in hibernate source code
Net. sf. hibernate .*

The classes of this package are basically interface classes and exception classes.

Net. sf. hibernate. cache .*

Implementation class of jcs

Net. sf. hibernate. cfg .*

Configuration File Reading Class

Net. sf. hibernate. collection .*

Hibernate collection interface implementation class, such as list, set, bag, etc. hibernate needs to write the collection interface implementation class to support lazy loading

Net. sf. hibernate. connection .*

Several Database Connection Pool providers

Net. sf. hibernate. dialect .*

Multiple database features are supported. Each dialect implementation class represents a database that describes the supported data types and other features of the database, such as autoincrement, sequence, and paging SQL.

Net. sf. hibernate. eg .*

Examples used in the hibernate document

Net. sf. hibernate. engine .*

The class functions of this package are scattered.

Net. sf. hibernate. expression .*

Expressions supported by hql

Net. sf. hibernate. hq .*

Hql implementation

Net. sf. hibernate. id .*

Id Generator

Net. sf. hibernate. impl .*

Core Package, implementation class of some important interfaces, if session, sessionfactory, query, etc.

Net. sf. hibernate. jca .*

Jca support. The session is packaged as an interface implementation class that supports jca.

Net. sf. hibernate. jmx .*

I don't understand jmx. I only know that jmx is used to compile the Management Program of the app server. It is probably the implementation of some jmx interfaces, so that the app server can manage hibernate through the jmx interface.

Net. sf. hibernate. loader .*

It is also a core package, mainly used to generate SQL statements.

Net. sf. hibernate. lob .*

Blob and clob support

Net. sf. hibernate. mapping .*

Attribute Implementation of hbm files

Net. sf. hibernate. metadata .*

Meta implementation of po

Net. sf. hibernate. odmg .*

Odmg Is An orm standard. This package is an implementation class of the odmg standard.

Net. sf. hibernate. persister .*

Core Package to realize the ing between persistent objects and tables

Net. sf. hibernate. proxy .*

Support for proxy and lazy loading

Net. sf. hibernate. PS tutorial .*

This package is preparedstatment cache

Net. sf. hibernate. SQL .*

Generate a jdbc SQL statement package

Net. sf. hibernate. test .*

Test class, you can use junit to test hibernate

Net. sf. hibernate. tool. hbm2ddl .*

Use hbm configuration file to generate ddl

Net. sf. hibernate. transaction .*

Hibernate transaction implementation class

Net. sf. hibernate. type .*

Data Type of the Persistent Object attribute defined in hibernate

Net. sf. hibernate. util .*

Some tool classes have relatively different functions

Net. sf. hibernate. xml .*

Xml Data Binding

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.