Different between session.get() and session.load()
2015-09-22 12:54
639 查看
Often times, you will notice Hibernate developers mix use of
Different between
Actually, both functions are use to retrieve an object with different mechanism, of course.
1.
It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object.
If no row found , it will throws an
2.
It always hit the database and return the real object, an object that represent the database row, not proxy.
If no row found , it return
It’s about performance
Hibernate create anything for some reasons, when you do the association, it’s normal to obtain retrieve an object (persistent instance) from database and assign it as a reference to another object, just to maintain the relationship. Let’s go through some examples to understand in what situation you should use
1.
For example, in a
Output
In
2.
In above scenario,
Output
In
It will always return a proxy object with the given identity value, even the identity value is not exists in database. However, when you try to initialize a proxy by retrieve it’s properties from database, it will hit the database with select statement. If no row is found, a
session.get()
It will always return
session.get()and
session.load(), do you wonder what’s the different and when you should use either of it?
Different between session.get()
and session.load()
Actually, both functions are use to retrieve an object with different mechanism, of course.1. session.load()
It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object.If no row found , it will throws an
ObjectNotFoundException.
2. session.get()
It always hit the database and return the real object, an object that represent the database row, not proxy.If no row found , it return
null.
It’s about performance
Hibernate create anything for some reasons, when you do the association, it’s normal to obtain retrieve an object (persistent instance) from database and assign it as a reference to another object, just to maintain the relationship. Let’s go through some examples to understand in what situation you should use
session.load().
1. session.get()
For example, in a Stockapplication ,
Stockand
StockTransactions should have a “one-to-many” relationship, when you want to save a stock transaction, it’s common to declared something like below
Stock stock = (Stock)session.get(Stock.class, new Integer(2)); StockTransaction stockTransactions = new StockTransaction(); //set stockTransactions detail stockTransactions.setStock(stock); session.save(stockTransactions);
Output
Hibernate: select ... from mkyong.stock stock0_ where stock0_.STOCK_ID=? Hibernate: insert into mkyong.stock_transaction (...) values (?, ?, ?, ?, ?, ?)
In
session.get(), Hibernate will hit the database to retrieve the
Stockobject and put it as a reference to
StockTransaction. However, this save process is extremely high demand, there may be thousand or million transactions per hour, do you think is this necessary to hit the database to retrieve the
Stockobject everything save a stock transaction record? After all you just need the Stock’s Id as a reference to
StockTransaction.
2. session.load()
In above scenario, session.load()will be your good solution, let’s see the example,
Stock stock = (Stock)session.load(Stock.class, new Integer(2)); StockTransaction stockTransactions = new StockTransaction(); //set stockTransactions detail stockTransactions.setStock(stock); session.save(stockTransactions);
Output
Hibernate: insert into mkyong.stock_transaction (...) values (?, ?, ?, ?, ?, ?)
In
session.load(), Hibernate will not hit the database (no select statement in output) to retrieve the
Stockobject, it will return a
Stockproxy object – a fake object with given identify value. In this scenario, a proxy object is enough for to save a stock transaction record.
Exception
In exception case, see the examplessession.load() Stock stock = (Stock)session.load(Stock.class, new Integer(100)); //proxy //initialize proxy, no row for id 100, throw ObjectNotFoundException System.out.println(stock.getStockCode());
It will always return a proxy object with the given identity value, even the identity value is not exists in database. However, when you try to initialize a proxy by retrieve it’s properties from database, it will hit the database with select statement. If no row is found, a
ObjectNotFoundExceptionwill throw.
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.mkyong.common.Stock#100]
session.get()
//return null if not found Stock stock = (Stock)session.get(Stock.class, new Integer(100)); System.out.println(stock.getStockCode()); //java.lang.NullPointerException
It will always return
null, if the identity value is not found in database.
Conclusion
There are no always correct solution, you have to understand the differential in between, and decide which method is best fix in your application.相关文章推荐
- Hibernate Oracle sequence的使用技巧
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- JAVA+Hibernate 无限级分类
- SSH整合中 hibernate托管给Spring得到SessionFactory
- jsp hibernate 数据保存操作的原理
- hibernate中的增删改查实现代码
- 解决hibernate+mysql写入数据库乱码
- java优化hibernate性能的几点建议
- java Hibernate延迟加载
- hibernate 常用方法介绍
- 深入理解Hibernate中的flush机制
- 简单的手工hibernate程序示例
- 解析使用jdbc,hibernate处理clob/blob字段的详解
- 浅析java程序中hibernate的应用总结
- java面试常见问题之Hibernate总结
- 关于Hibernate的一些学习心得总结
- Hibernate管理Session和批量操作分析
- 基于spring+hibernate+JQuery开发之电子相册(附源码下载)
- java Hibernate 一对多自身关联问题