Analysis on the Application of hibernate in java programs

Source: Internet
Author: User

Hibernate can be understood as a middleware that receives the SQL statements of the java program and sends them to the database. After receiving the information returned by the database, hibernate directly generates an object and transmits it to java.

We know that if we use java to connect to the database and have a large amount of SQL code, we want to use a tool to operate the database,
The first thing we think of is JDBC. But to better operate on class attributes, we will choose Hibernate.

So what is the principle of Hibernate?
Hibernate can be understood as a middleware that receives the SQL statements of the java program and sends them to the database,
After receiving the information returned by the database, hibernate directly generates an object and transmits it to java.

First, let's take a look at the seven processes of hibernate's work:
1. Read and parse the configuration file
2. Create SessionFactory
3. open the Session
4. Start a transaction
5. Persistent operations
6. Submit the transaction
7. Disable Session

Second, we need to understand that the life cycle of an object is the three states of Hibernate.
Transient status (Transient)
Just created with the new statement, it has not been persisted and is not in the Session cache.
Persistent)
Already persisted and added to the Session cache
Detached)
It has been persisted but is no longer in the Session cache

III persistence and ORM are also part of what Hibernate needs to understand.
Persistence: Data Model (memory) "--" Storage Model (persistence device). Persistence in Java usually refers to using SQL to store data in relational databases.
ORM is a persistent solution. It maps object models and relational database Relational Models and describes these mappings using metadata.

In Hibernate, you must build a project framework to compile the hibernate configuration file and object ing file.
Create a project and import the jar package
Create a Hibernate configuration file
Used to configure database connections
Various runtime attributes

The default file name is "hibernate. cfg. xml"
Test connection
Use Hibernate to add, delete, and modify data
1. Create an object class
2. Create and configure a ing File
3. Primary Key ing
4. Primary Key Generation Policy
5. Add the object ing file path to the main configuration file.
6. Database Operations
7. add, delete, modify, and query a single table

4. Hibernate supports two main query methods
(1) HQL (Hibernate Query using GE, Hibernate Query language)
Query
It is an Object-Oriented Query Language, which does not have the concepts of tables and fields, but only the concepts of classes, objects, and attributes.
HQL is widely used.

(2) Criteria Query
Also known as "Object Query", it encapsulates the process of constructing a query in an object-oriented manner.

5. When using Hibernate, we should pay attention to some things
(1) How does Hibernate delay loading?
1. Implementation of Hibernate delayed loading: a) object B) Collection)
2. Hibernate3 provides the property's delayed Loading Function
When Hibernate queries data, the data does not exist in the memory. When the program actually operates on the data, the object exists in the memory, and the loading is delayed, it saves the server memory overhead and improves the server performance.

(2) discuss the cache mechanism of Hibernate
1. The internal cache exists in Hibernate, which is also called a level-1 cache, which belongs to the application thing-level cache.
2. Secondary cache:
A) Application and Cache 
B) distributed cache
Condition: the data is not modified by a third party, the data size is acceptable, the data update frequency is low, and the same data is frequently used by the system,
Non-critical data
C) third-party cache implementation

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.