1

Hibernate中Session的get和load

 1 year ago
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

精选 原创

是温度呀 2022-09-22 11:16:44 ©著作权

文章标签 sql语句 主键 hibernate 文章分类 Java 编程语言 阅读数160

hibernate中Session接口提供的get()和load()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别。测试版本:hibernate 4.2.0。

Session接口提供了4个重载的get方法,分别通过“持久类+主键”和“全类名+主键”以及“锁选项”来获取实体对象。

public Object get(Class clazz, Serializable id);
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);
1 @Test
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查询语句,并返回结果。

Hibernate中Session的get和load_主键

load跟get调用方式基本一样,多了一个重载方法:使用“一个空的持久化类的实例+主键”来获取实体对象。

public void load(Object object, Serializable id);
1 @Test
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语句。

1 @Test
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 }
Hibernate中Session的get和load_hibernate_02

分别将打印代码放在session关闭之后,load方法抛出异常:org.hibernate.LazyInitializationException。

1 @Test 2 public void testLoad() throws Exception {
3 Session session = sessionFactory.openSession();
4
5 Student student = (Student) session.load(Student.class, 1L);
6 session.close();
7 System.out.println(student);
8 }
Hibernate中Session的get和load_主键_03

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

②查询数据库中不存在的数据时,get方法返回null,load方法抛出异常:org.hibernate.ObjectNotFoundException。

Hibernate中Session的get和load_sql语句_04

如果我们使用load方法查询数据库中不存在的数据,且在session关闭之后,获取对象的id属性,结果会怎样呢?

1 @Test
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 }
Hibernate中Session的get和load_sql语句_05

结果印证了:调用load方法返回的是代理对象,代理对象中只保存了id,只有在获取非主键属性时,才会发出sql语句;因为没有执行sql语句,所以没有抛出ObjectNotFoundException异常。

关于延迟加载和缓存

上面的测试结果都是因为hibernate3以后默认启用了延迟加载:lazy="true",get方法不使用延迟加载机制,首先查找Session缓存,然后查找二级缓存,然后查询数据库(hibernate3以前会跳过二级缓存,直接查询数据库)。load方法首先查找Session缓存,然后查找二级缓存,若找不到则返回代理对象,延迟到真正使用对象非主键属性时才发出sql语句加载对象。

若设置lazy="false",load返回代理对象之后会发出sql语句,若找不到符合条件的记录,依然会抛出ObjectNotFoundException异常。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK