How Hibernate works

Source: Internet
Author: User
Tags xml parser

How 1.Hibernate connects to a database

Primarily through configuration in the Hibernate.cfg.xml configuration file

This file defines the information needed to connect the database, including the JDBC driver, user name, password, database dialect, etc.

The configuration class parses the settings environment with DOM4J's XML parser and then uses these environment properties to generate the sessionfactory. So this

A sessionfactory generated session will successfully connect to the database.

How 2.Hibernate is database write operations

Write operations to the database include save, update, and delete, triggering Hibernate's Save event listener when saving a Pojo persistent object

Be processed. Hibernate uses the mapping file to obtain the name of the object's corresponding database table and the column names in the table corresponding to the property, and then through the reflection mechanism

The individual properties of the persisted object (the entity object) are eventually organized into SQL INSERT statements that insert new objects into the database. Called the

Session.save () method, this object will be identified as persisted in the session, and for hibernate it is a persistent

The object, but hibernate does not actually execute the INSERT statement when the session is refreshed with the same part or the transaction commits.

Hibernate executes all the SQL statements in the session cache, and a similar mechanism is used for update and delete operations.

Then, once the transaction is committed and the transaction commits successfully, the writes are permanently saved into the database, so the session is used for the data

The library operation also relies on the processing of hibernate transactions. If a level two cache is set, these operations are synchronized to the level two cache, and Hibernate

The final operation on the database is also dependent on the underlying JDBC for the database.

3.Hibernate How to load objects from a database

When loading an object using Session.load (), you can set whether lazy loading is used, and if lazy loading, then the object returned by load actually

Is the proxy class returned by Cglib or Javassist, and its non-primary key properties are empty, which is useful for object collection properties. Hibernate takes it.

Save memory, Hibernate attempts to read from the session cache first when the object is really needed, if the data does not exist in the session cache

or dirty data and configured with a level two cache, Hibernate attempts to retrieve data from the level two cache, otherwise hibernate will be based on the object type, the main

The key and other information organizes the SELECT statement to read in the data, and then organizes the select results back into objects.

4.Hibernate How to perform database query operations

Hibernate provides the SQL HQL criteria Query method. HQL is one of the most widely used query methods. Users to use

After the Session.createquery () method creates a query object with a HQL statement for the parameter, Hibernate uses the ANLTR library to put the HQL statement

resolves to a SQL statement that can be identified by JDBC, and if the query cache is set, Hibernate will cache the query before executing query.list ()

Query, and then use the SELECT statement to query the database if the query cache does not exist.


http://tiantiankaixin.iteye.com/blog/776822

How Hibernate works

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.