Brief analysis of application of hibernate in Java program

Source: Internet
Author: User
Tags object model
Hibernate can be understood as a middleware it is responsible for the Java program to receive SQL statements sent to the database, and the information returned by the database after hibernate received directly to generate an object to the Java

We know that if we use Java to connect to a database, a lot of SQL code, we want to manipulate the database with a tool,
The first thing we think of is JDBC, but we want to do better with the attributes of the class, we will choose Hibernate.

So what is the hibernate principle?
hibernate can be understood as a middleware it is responsible for the Java program to receive SQL statements sent to the database,
And the information returned by the database hibernate received directly after receiving the object passed to Java.

First, let's take a look at Hibernate's work 7 processes:
1. Read and resolve configuration files
2. Create Sessionfactory
3. Open session
4. Start a thing
5. Persistent operation
6. Submission of services
7. Close session

Second, we want to understand that the life cycle of entity objects is three states of hibernate
instantaneous State (Transient)
has just been created with the new statement and has not been persisted and is not in the session cache
Persistent State (persistent)
has been persisted and added to the session's cache
Free State (Detached)
has been persisted, but is no longer in the session's cache

Three persistence and ORM are also part of the hibernate that needs to be understood.
Persistence: Data Model (Memory) "-" Storage model (persistent device), which is said to be persistent in Java, usually refers to the use of SQL to store data in a relational database.
ORM is a persistent solution that maps the object model and relational database relational models, and describes them using meta data.

Hibernate must build the project framework to write Hibernate configuration files, entity mapping files
To create a project and import a jar package
Creating a Hibernate configuration file
Used to configure a database connection
Various properties that are required at run time

The default file name is "Hibernate.cfg.xml"
Test Connection
Use hibernate to complete data deletion and modification operation
1. Create entity classes
2. Create and configure mapping files
3. Primary KEY Mapping
4. Primary key generation Policy
5. Add the Entity mapping file path to the main configuration file
6. Database Operations
7. Change and check of single watch

Four Hibernate support two main ways of querying
(1) HQL Hibernate Query languge,hibernate Query Language

is an object-oriented query language in which there is no concept of tables and fields, only the concepts of classes, objects, and attributes
HQL is a more widely used approach.

(2) Criteria query
also known as an object query, it encapsulates the process of constructing queries in an object-oriented manner.

When using hibernate, we should pay attention to some things
(1) How is hibernate delayed loading?
1.Hibernate deferred load implementation: a) collection of entity object B (Collection)
2.hibernate3 provides deferred loading of properties
When the hibernate is querying the data, the data does not exist in memory, when the program is actually operating on the data, the object exists and in memory, it realizes the delay loading, saves the memory cost of the server, and improves the performance of the server.

(2) Next Hibernate's caching mechanism
1. Internal cache exists in the hibernate is also called the first level cache, belongs to the application thing level cache
2. Level Two cache:
a) application and caching
b) Distributed caching
conditions: The data will not be modified by the third party, the data size in the acceptable range, the data update frequency is low, the same data is frequently used by the system,
Non-critical data
( c) Implementation of third party caching

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.