Principle and application of Hibernate

  • 2020-06-07 04:27:04
  • OfStack

Hibernate works and why ? is used;

Principle:

1. Through the Configuration (.) configure (); Read and parse the hibernate.cfg.xml configuration file

2. From hibernate.cfg.xml < mapping resource="com/xx/User.hbm.xml"/ > Read and parse the mapping information

3. Through config. buildSessionFactory (); / / create SessionFactory

4. sessionFactory. openSession (); / / open Sesssion

5. session. beginTransaction (); // Create transaction Transation

6.persistent operate persistence operation

7. session. getTransaction (.) commit (); // Commit the transaction

8. Close Session

9. Close SesstionFactory

Why:

1. Encapsulate the code of JDBC accessing database, which greatly simplifies the tedious repetitive code of data access layer.

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

hibernate USES the Java reflection mechanism instead of bytecode enhancers to achieve transparency.

hibernate performs very well because it is a lightweight framework. The flexibility of the mapping is excellent. It supports a variety of relational databases, ranging in complexity from one-to-one to many-to-many.

2. How Hibernate is lazy loaded ?

1. Hibernate2 Lazy loading implementation: a) Entity Object b) Collection (Collection)

2. Hibernate3 provides lazy loading of attributes

When Hibernate is querying the data, the data does not exist in memory; when the program really operates on the data, the object exists in memory, then lazy loading is realized. It saves the memory overhead of the server, thus improving the performance of the server.

3. How to implement relationships between classes in Hibernate ? (e.g. 1 to many, many to many relationship)

The relationship between classes is mainly reflected in the operation between tables. They operate on objects. In our program, all tables and classes are mapped to 1.

4. Talk about the caching mechanism of Hibernate

1. Internal cache exists in Hibernate, also known as level 1 cache, which belongs to application thing cache

2. Level 2 cache:

a) application and cache

b) distributed cache

Conditions: the data will not be modified by the third party, the data size is in an acceptable range, the data update frequency is low, the same data is frequently used by the system, non-critical data

c) implementation of the third side cache

Level 1 cache: An session level cache, also known as a transaction level cache, caches only entities, life cycles, and session1. It cannot be managed.

Calls that are not displayed.

Level 2 cache: The sessionFactory cache, also known as a process-level cache, is implemented using the third party plugin and is also valued for cache entities, life cycle and sessionFactory1, which can be managed.

First configure the third plugin. We used EHCache and added it to the ES129en.cfg.xml file

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

The call that will also be shown in the map, < cache usage="read-only"/ >

Query cache for level 2 cache: caches common attributes. If the associated table changes, the query cache life cycle is over.

Query caching must be manually enabled in the program: ES144en. setCacheable(true); /////////

5. Hibernate query mode

Sql, Criteria object comptosition

Hql:

1. Attribute query

2. Parameter query and named parameter query

3. Associate query

4, paging query

5. Statistical function

6. How to optimize Hibernate?

1. Use two-way one-to-many association instead of one-way one-to-many

2. Flexible use of one-way one-to-many associations

3. Instead of 1 versus 1, replace it with many-to-1

4. Configure object cache without using collection cache

5.1 Use Bag for multiple collections and Set for multiple collections

6. Inherited classes use explicit polymorphism

7. Table fields should be less, table associations should not be afraid of more, with the support of level 2 cache

1. What are the ways of Hibernate to query data

(1) Navigation object graph query

(2) OID queries

(3)HQL

(4)QBC

(5) the local SQL

2. The difference between load() and get(

load Loading 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 object for userId in the database. If loaded via the get method, 1 null is returned; If loaded via load, 1 proxy object is returned, and if the following code calls a property of the user object (such as user.getPassword ()), an exception is thrown: org.hibernate.ObjectNotFoundException;

Difference 2: load supports lazy loading, get does not.


Related articles: