Hibernate Basic Learning (ii)-hibernate related API Introduction

Source: Internet
Author: User

First, Hibernate's core interface

All hibernate applications will have access to Hibernate's 5 core interfaces.

(1) Configuration Interface: Configure Hibernate, start Hibernate, create Sessionfactory objects.

(2) Sessionfactory interface: Initialize Hibernate, create session.

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

(4) Transaction interface: Managing transactions.

(5) Query and Criteria interface: Execute a database query.

1.Configuration interface

The configuration object is used to configure and Root initiates hibernate. Hibernate applications use configuration instances to get the metadata in the object-relational mapping file, as well as dynamically configure Hibernate properties, and then create sessionfactory instances.

the configuration class is responsible for managing Hibernate config information. as follows:
Hibernate runs the underlying information: database URL, user name, password, JDBC driver class, database dialect, database connection pool, etc. (corresponding to Hibernate.cfg.xml file).
The mapping of persisted classes to data tables: (*.hbm.xml file).

There are two ways to create a configuration:

Mode 1: properties File-hibernate.properties

Configuration cfg = new configuration ();

Mode 2: XML file-hibernate.cfg.xml

Configuration cfg = new configuration (). Configure ();

The Configure () method also supports access with parameters.

File File = new file ("Simpleit.xml"); Configuration cfg = new configuration (). Configure (file);

2.SessionFactory interface

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

(1) It is thread-safe. This means that the same instance of it can be shared by multiple threads that are applied.

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

The reason Sessionfactory is called heavyweight is because it requires a large cache to hold predefined SQL statements and map metadata. Users can also configure a cache plug-in for Sessionfactory, which is called Hibernate's second-level cache, which is used to hold data that is read by the unit of work, which may be reused by other units of work, so that the data in the cache can be shared by all units of work. A unit of work typically corresponds to a database transaction.

3.Session interface

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

The session has the following features:

(1) is not thread-safe. Therefore, you should avoid multiple threads sharing the same session instance at design time.

(2) The session instance is lightweight. the so-called lightweight refers to its creation and destruction without consuming too much resources. This means that the session object can be created or destroyed frequently in the program, such as assigning individual session instances to each client request.

The session has a cache that is called Hibernate's first-level cache, which holds objects loaded in the current unit of work. Each session instance has its own cache, and the cache of the session instance can only be accessed by the current unit of work.

4.Transaction interface

The transaction interface is the Hibernate database transaction interface, which encapsulates the underlying transactional interface, including the following:

(1) JDBC API

(2) JTA (Java Transaction API)

(3) CORBA (Common Object Request Broker Architecture) API

Hibernate applications can declare transactions through a consistent transaction interface, which helps the application migrate in different environments or containers.

5.Query and Criteria Interface

The query and Criteria interface is the lookup interface for Hibernate, which is used to query the database for objects and to control the process of executing queries. The query instance wraps a HQL query statement, which is somewhat similar to the SQL query statement, but the HQL query statement is object-oriented, referencing the class name and the property name of the class instead of the table name and field name. The criteria interface completely encapsulates query statements based on string forms, which are more object-oriented than the query interface, and the criteria interface excels at executing dynamic queries.

Hibernate Basic Learning (ii)-hibernate related API Introduction

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.