Hibernate notes ----------- core interfaces of Hibernate

Source: Internet
Author: User

Hibernate notes ----------- core interfaces of Hibernate


There are five core interfaces:

1) Configuration interface: Configure Hibernate, start Hibernate at the root, and create the SessionFactory object.

Hibernate applications use Configuration to obtain the object-the metadata of the relational ing file and dynamically configure the attributes of Hibernate to create the SessionFactory object.

2) SessionFactory interface: initializes Hibernate and acts as a data source proxy to create a Session object.

A SessionFactory corresponds to a data source and obtains the Session instance from SessionFactory:

SessionFactory is thread-safe, indicating that the same instance can be shared by multiple threads of the application.

It is heavyweight and cannot be created or destroyed at will. If the application only accesses the same database, you only need to create a SessionFactory instance and create the instance during application initialization. If the application accesses multiple databases at the same time, create a separate SessionFactory instance for each database.

SessionFactory is heavyweight because it has a large cache to store predefined SQL statements and ing metadata. You can configure a cache plug-in for SessionFactory, called a second-level cache, to store the data read by the work unit. In the future, other work units may use the data, therefore, this cache is shared by all work units. A unit of work usually corresponds to a transaction.

3) Session interface: stores, updates, deletes, loads, and queries objects.

The persistence manager provides persistence-related operations. It has the following features:

Threads are not secure. Avoid multiple threads sharing a Session instance.

Lightweight. Creating and destroying a Session object does not consume too much resources, which means that the Session object is often created and destroyed in the program. For example, you can allocate a separate Session instance for each customer request or assign a separate Session instance for each work unit.

The Session has a level-1 cache, which stores the objects loaded by the current unit of work. Each Session instance has its own cache. the cache of this Session instance can only be accessed in the current unit of work.

4) Transaction Interface

Is the transaction interface of Hibernate, which encapsulates the underlying transactions. The underlying transaction interfaces include:

JDBC

JTA

CORBA API

The Hibernate application declares the Transaction boundary through the consistent Transaction interface, which helps the application to be transplanted in different environments or containers.

5) Query and Criteria interfaces: Execute database queries

The Query and Criteria interfaces are the Hibernate Query interfaces used to Query objects in the database and control the Query execution process. The Query instance encapsulates an object-oriented HQL statement, which references the class name and attribute name instead of the table name and field name. The Criteria interface completely encapsulates Query statements in the string format, which is more object-oriented than the Query interface. The Criteria interface is good at executing dynamic Query statements.


Event processing interface:

When the program loads, saves, updates, or deletes objects through Hibernate, The Hibernate interceptor and event listener are triggered to handle the problem accordingly:

Event and event listener interfaces:

In the Hibernate API, there is an event listener for each event. For example, loading an object triggers org. hibernate. event. loadEvent event, which is composed of org. hibernate. event. loadEventListener listener processing...

Org. hibernate. Interceptor interface:

Applications can define classes that implement Interceptor interfaces. Interceptor implementation classes are responsible for responding to events that are loaded, saved, updated, or deleted for persistent classes.


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.