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

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

问题描述

从 API 中,我可以看到它与代理有关.但是我找不到很多关于代理的信息,也没有理解调用session.getsession.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?

谢谢!!

推荐答案

来自 Hibernate 论坛:

这来自 Hibernate in Action 一书.好一个读这个..

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

<小时>

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


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

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

get() 方法很特别,因为标识符唯一地标识了单个一个类的实例.因此,应用程序通常将标识符用作持久化对象的便捷句柄.通过标识符检索可以使用缓存检索对象时,如果对象已被缓存,则避免数据库命中.Hibernate 还提供了一个 load() 方法:

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);

load() 方法较旧;由于用户,get() 被添加到 Hibernate 的 API 中要求.区别很简单:

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

如果 load() 在缓存或数据库中找不到对象,一个异常是抛出.load() 方法从不返回 null.get() 方法返回如果找不到对象,则为 null.

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.

load() 方法可能返回一个代理而不是一个真正的持久实例.代理是一个占位符,当它被加载时触发真实对象的加载第一次访问;在另一方面, get() 从不返回代理.在 get() 和 load() 之间进行选择很容易:如果您确定持久对象存在,不存在将被视为例外,load() 是一个不错的选择.如果您不确定是否有给定的持久实例标识符,使用 get() 并测试返回值以查看它是否为空.使用 load() 有进一步的含义:应用程序可以检索到一个有效的引用(代理)持久化实例,而无需访问数据库来检索其持久化状态.所以load() 在找不到持久对象时可能不会抛出异常在缓存或数据库中;稍后会抛出异常,当代理被访问.当然,通过标识符检索对象并不像使用任意查询.

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天全站免登陆