Principles and Applications of Hibernate

Source: Internet
Author: User

Principles and Applications of Hibernate
How does Hibernate work and why?

Principle:
1. Read and parse the hibernate. cfg. xml Configuration file through Configuration (). configure ();
2. The ing information is read and parsed by <mapping resource = "com/xx/User. hbm. xml"/> In hibernate. cfg. xml.
3. Create SessionFactory through config. buildSessionFactory (); //
4. sessionFactory. openSession (); // open Sesssion
5. session. beginTransaction (); // create transaction Transation
6. persistent operate persistent operation
7. session. getTransaction (). commit (); // submit the transaction
8. Disable Session
9. Disable SesstionFactory

Why:
1. The code for JDBC database access is encapsulated, which greatly simplifies the tedious and repetitive code at the data access layer.

2. Hibernate is a mainstream persistence framework based on JDBC and an excellent ORM implementation. He greatly simplifies the coding of the DAO layer.

3. hibernate uses the Java reflection mechanism instead of the bytecode enhancement program to achieve transparency.

4. hibernate has excellent performance because it is a lightweight framework. The flexibility of ing is excellent. It supports various relational databases, from one-to-one to many-to-many complex relationships.
 
2. How does Hibernate delay loading?
1. Implementation of Hibernate2 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.

3. How does one implement the relationship between classes in Hibernate? (For example, one-to-many and many-to-many relationships)

The relationship between classes is mainly reflected in the relationship between tables. They operate on objects in a city. In our program, all tables and classes are mapped together, they are used in the configuration file named "allow-to-one", "one-to-allow", "allow-to-allow,

4. Talk about 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 within the acceptable range, the data update frequency is low, the same data is frequently used by the system, non-critical data
C) third-party cache implementation
Level-1 cache: session-level cache is also called transaction-level cache. It only caches entities and has the same lifecycle as session. It cannot be managed.

Do not display the call.

Level-2 Cache: sessionFactory cache, also known as process-level cache. It is implemented using the 3rd-party plug-in and also a value-based Cache entity. The lifecycle is consistent with that of sessionFactory and can be managed.

First, configure the 3rd plug-in. We use EHCache and add it to the hibernate. cfg. xml file.

<Property name = "hibernate. cache. user_second_level_cache"> true </property>

Call to be displayed in the ing, <cache usage = "read-only"/>

Second-level cache query cache: caches common attributes. If the associated table is modified, the query cache lifecycle ends.

You must manually enable query cache in the program: query. setCacheable (true );/////////

5. Hibernate query method
SQL, Criteria, object comptosition
Hql:
1. query attributes
2. parameter query and name parameter query
3. join query
4. Paging Query
5. Statistical functions

6. How to optimize Hibernate?
1. Use bidirectional one-to-Multiple Association instead of unidirectional one-to-Multiple Association
2. flexible use of one-to-multiple associations
3. Replace multiple-to-one instead of one-to-one
4. Configure the object cache without using the set Cache
5. Use Bag for one-to-multiple sets and Set for multiple-to-multiple sets
6. Use explicit polymorphism for inheritance classes
7. Fewer table fields, no more table Association, and secondary cache support


1. What data query methods does Hibernate use?

(1) navigation object graph Query

(2) OID Query

(3) HQL

(4) QBC

(5) Local SQL

2. Differences between load () and get ()

 

Load Method:

Java code

Users user = (Users) session. load (Users. class, userId );

Users user = (Users) session. load (Users. class, userId );

Get loading method:

Java code

Users user = (Users) session. get (Users. class, userId );

Users user = (Users) session. get (Users. class, userId );

 

Differences between the two loading methods:

Difference 1: If there is no userId object in the database. If the get method is used for loading, a null value is returned. If the load method is used for loading, a proxy object is returned. If the following code calls an attribute of the user object (such as user. getPassword () throws an exception: org. hibernate. objectNotFoundException;

Difference 2: load supports delayed loading, while get does not support delayed loading.

Working principle diagram

 

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.