

Hibernate中Session的get和load
source link: https://blog.51cto.com/u_15776483/5702628
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hibernate中Session的get和load
精选 原创hibernate中Session接口提供的get()和load()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别。测试版本:hibernate 4.2.0。
Session接口提供了4个重载的get方法,分别通过“持久类+主键”和“全类名+主键”以及“锁选项”来获取实体对象。
public Object get(Class clazz, Serializable id, LockOptions lockOptions);
public Object get(String entityName, Serializable id);
public Object get(String entityName, Serializable id, LockOptions lockOptions);
2 public void testGet() throws Exception {
3 Session session = sessionFactory.openSession();
4
5 Student student = (Student) session.get(Student.class, 1L);
6 // Student student = (Student)session.get("com.lt.crm.entity.Student", 1L);
7 System.out.println(student);
8
9 session.close();
10 } ```
向数据库发出一条sql查询语句,并返回结果。

load跟get调用方式基本一样,多了一个重载方法:使用“一个空的持久化类的实例+主键”来获取实体对象。
2 public void testLoad() throws Exception {
3 Session session = sessionFactory.openSession();
4
5 Student student = new Student(); 6 session.load(student, 1L);
7 System.out.println(student);
8
9 session.close();
10 }
get和load的区别
分别注释掉打印语句,即只是获取了对象,没有使用该对象。可以看到get方法发出了sql语句,而load方法没有发出sql语句。
2 public void testGet() throws Exception {
3 Session session = sessionFactory.openSession();
4
5 Student student = (Student) session.get(Student.class, 1L);
6 // System.out.println(student);
7 session.close();
8 }

分别将打印代码放在session关闭之后,load方法抛出异常:org.hibernate.LazyInitializationException。
3 Session session = sessionFactory.openSession();
4
5 Student student = (Student) session.load(Student.class, 1L);
6 session.close();
7 System.out.println(student);
8 }

①get方法会在调用之后立即向数据库发出sql语句(不考虑缓存的情况下),返回持久化对象;而load方法会在调用后返回一个代理对象,该代理对象只保存了实体对象的id,直到使用对象的非主键属性时才会发出sql语句。
②查询数据库中不存在的数据时,get方法返回null,load方法抛出异常:org.hibernate.ObjectNotFoundException。

如果我们使用load方法查询数据库中不存在的数据,且在session关闭之后,获取对象的id属性,结果会怎样呢?
2 public void testLoad() throws Exception {
3 Session session = sessionFactory.openSession();
4
5 Student student = (Student) session.load(Student.class, 4L);
6 session.close();
7 System.out.println(student.getId());
8 }

结果印证了:调用load方法返回的是代理对象,代理对象中只保存了id,只有在获取非主键属性时,才会发出sql语句;因为没有执行sql语句,所以没有抛出ObjectNotFoundException异常。
关于延迟加载和缓存
上面的测试结果都是因为hibernate3以后默认启用了延迟加载:lazy="true",get方法不使用延迟加载机制,首先查找Session缓存,然后查找二级缓存,然后查询数据库(hibernate3以前会跳过二级缓存,直接查询数据库)。load方法首先查找Session缓存,然后查找二级缓存,若找不到则返回代理对象,延迟到真正使用对象非主键属性时才发出sql语句加载对象。
若设置lazy="false",load返回代理对象之后会发出sql语句,若找不到符合条件的记录,依然会抛出ObjectNotFoundException异常。
Recommend
-
139
9 High-Performance Tips when using MySQL with JPA and Hibernate Last modified: Nov 27, 2021 Imagine having a tool that can automatically detect JPA and Hibernate...
-
153
Please wait... We are checking your browser... www.baeldung.com What can I do to prevent this in t...
-
115
Building Restful APIs with Kotlin, Spring Boot, Mysql, JPA and HibernateRajeev SinghSpring BootOctober 06, 20173 mins read...
-
96
Introduction The open-source hibernate-types project allows you to map Java objects or Jackson JsonNode as JPA entity properties. Recently, thanks to our awesome contributors, we added support for type-safe collections to be persisted as JSON as...
-
86
Hibernate is the predominant Java ORM, with rich support for multi-tenant apps. Hibernate supports several multi-tenancy methods, including tenant discriminator, which is the best for scaling. With...
-
7
#Featured #Android12 #GoogleIOTop 12 tips...
-
5
Failed to get a session component inside AppController advertisements I need to get the roles of a user from the database. I want to...
-
10
Get the function pointer to run in a shared library I did not load directly advertisements My Linux application (A) links a...
-
5
Framework Joy: Load in Hibernate Updates Data March 31, 2014 Would you ever guess that this line // Load Buyer fro...
-
10
Laravel Set and Get Session Example 3700 views 1 year ago Laravel In a programming language, session is a great way...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK