Hibernate Session.load()何时抛出异常 [英] When does Hibernate Session.load() throw an exception
问题描述
try {
x = session.load(...);
y = x;
catch(例外e){
//做某事
}
如果该键不存在,将立即加载(...)抛出异常
- li>
- 会返回一个代理,然后当对象试图从db中获取时抛出一个异常
?
另外,在上面的代码中,如果执行达到赋值y = x,是否保证在那一点上x不为空?有没有这种情况下,在这一点上x可以为空?
简而言之: 详述: 您是否试图确定钥匙是否存在?或者你认为它是,但只是想处理异常? 请参阅 documentation ,具体为: 您不应该使用此方法来确定实例是否存在(改用 从这本书'hibernate'中可以看出,在使用load()方法的时候: 应用程序可以检索一个有效的引用(代理)到一个 所以: 如果您想知道该项是否存在,请使用
x
在加载后不会为空,所以 y
load()
可能有也可能没有例外
load()
的异常,并且该对象不存在,那么访问 x $ c $时会出现异常c>或
y
。
get()
)。
$ b
持久化实例而不会触击数据库来检索其持久状态。因此,
load()
在缓存或数据库中未找到持久对象
时可能不会引发异常;当代理
被访问时,会抛出异常。
get()
,而不是 load()
。
try {
x = session.load(...);
y = x;
}
catch(Exception e) {
//do something
}
If the key is not present, will load(...) throw an exception
- right away
- will return a proxy and then throw an exception when the object is trying to be obtained from the db later?
Also, in the code above, if the execution reaches the assignment y = x, is it guaranteed that at that point, x is not null? Are there situations where x can be null at that point?
In short:
x
will not be null after the load, so neither willy
- There may or may not be an exception from
load()
if the object doesn't exist - If there is no exception from
load()
, and the object doesn't exist, there will be an exception when accessingx
ory
.
To elaborate:
Are you trying to determine if the key is present? Or are you assuming it is, but just want to handle anomalies?
See the documentation, specifically:
You should not use this method to determine if an instance exists (use
get()
instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.
From the book 'hibernate in action' on using load():
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.
So:
If you are looking to know whether the item exists, use get()
, not load()
.
这篇关于Hibernate Session.load()何时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!