Hibernate lazy loading and caching pit How much do you know? These five simple questions can not say I will not answer with hibernate

 Question 1: After session.flush () call, but also lazy loading it into effect?

If you do not take effect, it is throwing an exception or no response, or directly return null?

The answer: to take effect. It can be understood in the same session which, lazy loading only once.

 

Question 2: Multiple calls navigation properties of the entity class, will be sql query multiple times?

If session.flush () call after that?

The answer: not sql query multiple times, even after session.flush.

 

Question 3: an entity newly created class object, when calling session.flush (), can be directly loaded navigation properties?

For example, the code below, whether directly from the database to the ID of roleId of Role objects?

User user = new User(......);
user.setRoleId(roleId);
db.save(user);
db.getSession().flush();
Role role = user.getRole();

 The answer: no.

 

Question 4: Multiple calls session.get (Serializable id) Will every query from the database? 

 

Answer: No, not just read from the cache, and returned each time turned out to be the same object! Note that the same memory address Oh!

 

Question 5: Do you know the ID of the same entity class (plus @Entity notes) each time you return to the same memory object?

The figure performed a total of three times sql, this is better understood, difficult to understand is: 3 employee is actually above the same memory address reference, that is completely identical to the same object! For example, I put employee1 name changed, so employee2 name also changed, really TM surprising!

 

Since the three employee figure inside of the same object, we can guess that it is because the addition of the Employee class @Entity notes, annotations and this will change the value of hashcode. If the query returns a DTO object is not the object should be different, right? On his way to verify this hypothesis, see figure below:

The figure illustrates: guessed it.

 

 

The figure illustrates: even if the second employee is found out from the database, but because it is the same with the first employee a memory object, so it's name attribute has been changed to the name of the values ​​in memory. The magic of it!

 

hibernate lazy loading sinkhole: For ManyToOne notes actually default is FetchType.EAGER

EAGER What does it mean? For example, the User class has a role attribute, when the user loads the object, the object will be loaded at the same time role, that is, you expect only a simple sql query from a user table, but the implementation of the two sql (or join queries).

So, usually for ManyToOne comment, you need to manually set the fetch = FetchType.LAZY, otherwise there will be serious performance problems.

 

Herein from the test frame jframe

Multi-level multi-module java web application framework built on spring mvc. Include: infrastructure layer, database definition specification, standardized database access, logging standard, multi-level exception caught, ajax standard specification, master pages norms, normative view showing, JavaScript framework norms. Indeed the extremely detailed specifications defined framework, such as a database definition layer: Use enumeration class, datetime / bool / string field specifications, for a plurality of 1,1, a plurality of pairs, many to many mapping specification foreign key relationship, parent class definition specification, specification comments field, and so lazy loading specification. . .

 

Technical exchange QQ group: 651 499 479, welcome java Great God to help, and also welcomes newcomers into the group learning.

 

github Address:  https://github.com/leotsai/jframe

 

THE END.

Original Address: https: //www.cnblogs.com/leotsai/p/five-questions-about-hibernate-cache-and-lazy-load.html

 Question 1: After session.flush () call, but also lazy loading it into effect?

If you do not take effect, it is throwing an exception or no response, or directly return null?

The answer: to take effect. It can be understood in the same session which, lazy loading only once.

 

Question 2: Multiple calls navigation properties of the entity class, will be sql query multiple times?

If session.flush () call after that?

The answer: not sql query multiple times, even after session.flush.

 

Question 3: an entity newly created class object, when calling session.flush (), can be directly loaded navigation properties?

For example, the code below, whether directly from the database to the ID of roleId of Role objects?

User user = new User(......);
user.setRoleId(roleId);
db.save(user);
db.getSession().flush();
Role role = user.getRole();

 The answer: no.

 

Question 4: Multiple calls session.get (Serializable id) Will every query from the database? 

 

Answer: No, not just read from the cache, and returned each time turned out to be the same object! Note that the same memory address Oh!

 

Question 5: Do you know the ID of the same entity class (plus @Entity notes) each time you return to the same memory object?

The figure performed a total of three times sql, this is better understood, difficult to understand is: 3 employee is actually above the same memory address reference, that is completely identical to the same object! For example, I put employee1 name changed, so employee2 name also changed, really TM surprising!

 

既然上图里面的三个employee是同一个对象,那我们可以猜测那是因为Employee类加了@Entity注解,而这个注解会改变hashcode的值。如果查询返回一个DTO对象,是不是就应该是不同的对象了吧?马上就来验证这个猜测,看下图:

上图说明:猜对了。

 

 

上图说明:即使第2个employee是从数据库查出来的,但是由于它跟第一个employee是同一个内存对象,所以它的name属性已经被改成了内存中的那个name值了。神奇吧!

 

hibernate懒加载天坑:对于ManyToOne注解居然默认是FetchType.EAGER

EAGER是什么意思呢?比如User类有个role属性, 当加载user对象的时候,就会同时加载role对象,也就是你期望的只有一条简单sql从user表查询,但是却执行了两条sql(或者是join查询)。

所以,通常情况下对于ManyToOne注解,都需要手工设置fetch = FetchType.LAZY,否则会出现严重性能问题。

 

本文中的测试来自jframe 框架

基于spring mvc搭建的多层级多模块java web应用程序框架。包含:基础设施层、数据库定义规范、数据库访问规范、日志记录规范、多层级异常捕获、标准ajax规范、母版页规范、视图呈现规范、JavaScript框架规范等。实际上该框架定义的规范极其详细,比如数据库定义层:枚举类使用规范、datetime/bool/string字段规范、1对1、1对多、多对1、多对多外键关系映射规范、父类定义规范、字段注释规范、懒加载规范等等。。。

 

技术交流QQ群:651499479,欢迎java大神指点迷津,也欢迎新手进群学习。

 

github地址: https://github.com/leotsai/jframe

 

THE END.

Original Address: https: //www.cnblogs.com/leotsai/p/five-questions-about-hibernate-cache-and-lazy-load.html

Guess you like

Origin www.cnblogs.com/jpfss/p/11058701.html