Core interface of Hibernate

Source: Internet
Author: User

All of the Hibernate applications will access the Hibernate 5 core interfaces.

Configuration Interface: Configure Hibernate, Root start Hibernate, create Sessionfactory object.

Sessionfactory interface: Initializes the hibernate, acts as the proxy for the data storage source, and creates the session object.

Session Interface: Responsible for saving, updating, deleting, loading, and querying objects.

Transaction: Managing Transactions.

Query and Criteria interface: executes a database query.

1.Configuration interface

The configuration object is used to configure and start the hibernate. Hibernate applies the configuration instance to specify the location of the object-relational mapping file or dynamically configure the Hibernate properties, and then create the Sessionfactory instance.

2.SessionFactory interface

A sessionfactory instance corresponds to a data storage source, and the application obtains the session instance from the Sessionfactory. Sessionfactory has the following characteristics:

It is thread-safe, which means that its same instance can be shared by multiple threads being applied.

It is heavyweight, which means that an instance of it cannot be created or destroyed at will. If the application accesses only one database, only one sessionfactory instance is created and the instance is created when the application is initialized. If the application accesses multiple databases at the same time, you need to create a separate sessionfactory instance for each database.

Sessionfactory is said to be heavyweight because it requires a large cache to hold predefined SQL statements to map metadata. Users can also configure a cache plug-in for Sesionfactory, which is referred to as the second-level cache of Hibernate. , the cache is used to hold data that has been read by the work unit, and other units of work may reuse it in the future, so the data in this cache can be shared by all work units. A unit of work usually corresponds to a database transaction.

3.Session interface

The session interface is the most widely used interface for hibernate applications. The session is also known as the Persistence Manager, which provides operations related to persistence, such as adding, updating, deleting, loading, and querying objects.

The session has the following characteristics:

is not thread-safe, you should avoid multiple threads sharing the same session instance when designing software schemas.

The session instance is lightweight and lightweight, meaning it does not need to consume too much resources to create and destroy. This means that session objects can be created and destroyed frequently in a program, such as assigning individual session instances to each client, or assigning individual session instances to each unit of work.

The session has a cache, called the first level cache of Hibernate, that holds objects loaded by the current unit of work. Each session instance has its own cache, and the cache of this sesion instance can only be accessed by the current unit of work.


4.Transaction interface

The transaction interface is a Hibernate database transaction interface that encapsulates the underlying transaction interface, which includes:

JDBC API, JTA (Java Transaction API), CORBA (Common Object requet Broker architecture) API

Hibernate applications can declare transaction boundaries through a consistent transaction interface, which can be used to migrate in different environment containers. Although applications can bypass the transaction interface and directly access the underlying transaction interface, this method is not recommended because it is not conducive to the application of porting in different environments.


5.Query and Criteria Interface

The query and criteria interfaces are hibernate queries that are used to query objects to the database and to control the process of executing the query. Query instance wraps a HQL query statement, HQL query statement and SQL query statement somewhat similar, but HQL query statement is object-oriented, it refers to the class sentence and the class of the property sentence, rather than the sentence and the table of the field sentence. The criteria interface fully encapsulates a string based query statement that is more object-oriented than the query interface and is adept at executing dynamic queries.

The session interface's find () method also has the data query function, but it only executes some simple HQL query statement's shortcut method, its function is far without the query interface formidable.


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.