JPA直接返回query.getSingleResult()时返回null
我对 hibernate 和 jpa 有一个非常奇怪的问题。下面是两个代码块:
public Object getObject(Date date) {
try {
Query query = entityManager
.createQuery(
"select ob from Object ob where date= :date");
query.setParameter("date", date);
return (Object)query.getSingleResult();
} catch (EmptyResultDataAccessException e) {
logger.debug(String.format("No Result found - date[%s]",date));
return null;
}
}
...
public Object getObject(Date date) {
try {
Query query = entityManager
.createQuery(
"select ob from Object ob where date= :date");
query.setParameter("date", date);
Object ret = (Object)query.getSingleResult();
return ret;
} catch (EmptyResultDataAccessException e) {
logger.debug(String.format("No Result found - date[%s]",date));
return null;
}
}
即使给出了日期匹配的有效记录,第一个代码块每次都会生成 EmptyResultDataAccessException。第二个返回预期的结果。有人遇到过这种情况吗?是什么导致了这种行为?
请假设所有其他语法事物都存在(事务、初始化的实体管理器等),我唯一更改的是查询结果是直接在返回中检索还是首先分配给变量。
I have a very strange issue with hibernate and jpa. Below are two blocks of code:
public Object getObject(Date date) {
try {
Query query = entityManager
.createQuery(
"select ob from Object ob where date= :date");
query.setParameter("date", date);
return (Object)query.getSingleResult();
} catch (EmptyResultDataAccessException e) {
logger.debug(String.format("No Result found - date[%s]",date));
return null;
}
}
...
public Object getObject(Date date) {
try {
Query query = entityManager
.createQuery(
"select ob from Object ob where date= :date");
query.setParameter("date", date);
Object ret = (Object)query.getSingleResult();
return ret;
} catch (EmptyResultDataAccessException e) {
logger.debug(String.format("No Result found - date[%s]",date));
return null;
}
}
The first generates an EmptyResultDataAccessException every time even given a valid record where the date matches. The second returns a result as expected. Has anyone encountered this? What causes this behavior?
Please assume all other syntatical things are present(a transaction, initialized entitymanager, etc) the only thing that I change is whether the query results are retrieved directly in the return or assigned to a variable first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有可能,我以前也遇到过。我认为这与 Hibernate 正在进行的字节码操作有关。要找到它的根源,你必须进入一个非常深入和黑暗的实施层面。
当使用 Hibernate/JPA 时,我总是使用第二种模式。不幸的是,因为它使代码变得更加冗长,但不值得深入研究生成的字节代码并尝试理解它。
It IS possible, I encountered it before as well. I think it has something to do with byte code manipulations that Hibernate is doing. To get to the root of it, you have to go to a very deep and dark level of implementation.
When working with Hibernate/JPA I always use the second pattern. It's unfortunate because it makes the code a bit more verbose, but it's not worth plunging into a depth of generated byte code and trying to comprehend it.
那是不可能的。这两个变体是等效的。也许你缺少一些东西。
请注意,如果结果超过 1 个,
getSingleResult()
可能会引发异常。That's not possible. The two variants are equivalent. There's something you are missing perhaps.
Note that
getSingleResult()
can throw an exception if there is more than 1 result.