gpt4 book ai didi

hibernate - 实体比较如何在 JPQL WHERE 子句中工作?

转载 作者:行者123 更新时间:2023-12-05 01:02:30 24 4
gpt4 key购买 nike

如何在 JPQL 中评估实体比较(相等)语句:是通过身份比较,还是通过 equals() 或其他?

我花了几个小时的谷歌搜索并浏览了 Hibernate 和 JPA 的规范,但仍然找不到它是如何工作的。考虑以下实体:

class MyProductType{Integer id;}
class MyProduct{Integer id; MyProductType pType;}

现在 JPQL/HQL 查询:
SELECT t FROM MyProductType t, MyProduct p WHERE p.pType = t

(我知道这是一个丑陋的查询,只需关注 where 子句语义。)

那么如何 p.pType = t评价?

JSR 317 提到了“entity_expression”比较,但它的行为没有阐明。

编辑:我不喜欢下面 Rika 的建议是 .id 方法包括隐式内部连接,这通常不是您想要的,以防查询使用外部(左)连接。

最佳答案

我在 http://www.objectdb.com/java/jpa/query/jpql/comparison 找到了这个,这很有趣,很好的问题。

Instances of user defined classes (entity classes and embeddable classes) can be compared by using the equality operators (=, <>, ==, !=). For entities, e1 = e2 if e1 and e2 have the same type and the same primary key value. For embeddable objects, e1 = e2 if e1 and e2 have exactly the same content.



所以它似乎检查对象的主键值和对象的类型。所以看起来p.pType = t,它会检查(假设id是主键)p.pType的id,t的id,看看它们是否相等。然后它将检查两个实体是否属于相同类型或 MyProductType。

关于hibernate - 实体比较如何在 JPQL WHERE 子句中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26300162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com