Hibernate:session.get和session.load之间的区别 [英] Hibernate: Difference between session.get and session.load

查看:92
本文介绍了Hibernate:session.get和session.load之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从API中,我可以看到它与代理有关。但是我找不到很多关于代理的信息,也不明白调用 session.get session.load 。可能有人请解释或指引我参考页?



谢谢!!

解决方案

来自 Hibernate论坛


本书来自Hibernate in Action。






通过标识符检索对象
以下内容Hibernate代码片段从数据库中检索User对象:

pre $ User user =(User)session.get(User.class,userID ); get()方法是特殊的,因为标识符唯一标识一个
实例的一个b




类。因此,应用程序通常使用标识符作为持久对象的
方便句柄。通过标识符检索可以在检索对象时使用缓存
,如果对象已被缓存,则避免数据库命中。
Hibernate还提供了一个load()方法:

$ p $ User user =(User)session.load(User.class , 用户名);

load()方法较旧;由于用户
请求,get()被添加到Hibernate的API中。差异是微不足道的:



如果load()在缓存或数据库中找不到对象,则会抛出异常
。 load()方法永远不会返回null。如果找不到对象,则get()方法返回
null。



load()方法可以返回代理,而不是真实的持久实例。
代理是一个占位符,它在首次访问
时触发加载实际对象;在
另一方面,get()永远不会返回代理。
在get()和load()之间进行选择很简单:如果您确定存在持久性
对象,并且不存在将被视为异常,则load()是一个
不错的选项。如果您不确定是否存在具有给定
标识符的持久实例,请使用get()并测试返回值以查看它是否为空。使用load()具有
还有一个含义:应用程序可以检索一个有效的引用(代理)到一个
持久化实例,而无需点击数据库来检索其持久化状态。因此,
load()在缓存或数据库中未找到持久对象
时可能不会引发异常;当代理
被访问时,将在稍后抛出异常。
当然,通过标识符检索对象不如使用任意
查询那么灵活。

From the API, I could see it has something to do with proxy. But I couldn't find a lot of information on proxy and do not understand the difference between calling session.get and session.load. Could someone please explain or direct me to a reference page?

Thank you!!

解决方案

From the Hibernate forum:

This from the book Hibernate in Action. Good one read this..


Retrieving objects by identifier The following Hibernate code snippet retrieves a User object from the database:

User user = (User) session.get(User.class, userID);

The get() method is special because the identifier uniquely identifies a single instance of a class. Hence it’s common for applications to use the identifier as a convenient handle to a persistent object. Retrieval by identifier can use the cache when retrieving an object, avoiding a database hit if the object is already cached. Hibernate also provides a load() method:

User user = (User) session.load(User.class, userID);

The load() method is older; get() was added to Hibernate’s API due to user request. The difference is trivial:

If load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found.

The load() method may return a proxy instead of a real persistent instance. A proxy is a placeholder that triggers the loading of the real object when it’s accessed for the first time; On the other hand, get() never returns a proxy. Choosing between get() and load() is easy: If you’re certain the persistent object exists, and nonexistence would be considered exceptional, load() is a good option. If you aren’t certain there is a persistent instance with the given identifier, use get() and test the return value to see if it’s null. Using load() has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So load() might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed. Of course, retrieving an object by identifier isn’t as flexible as using arbitrary queries.

这篇关于Hibernate:session.get和session.load之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆