JPA直接返回query.getSingleResult()时返回null

发布于 2025-01-03 18:56:26 字数 1162 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2025-01-10 18:56:26

可能,我以前也遇到过。我认为这与 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.

一曲爱恨情仇 2025-01-10 18:56:26

那是不可能的。这两个变体是等效的。也许你缺少一些东西。

请注意,如果结果超过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文