JOIN FETCH中缺少Hibernate @Where子句

m3eecexj  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(177)

我有以下3个实体:ItemWarehouseItemWarehouse,其中有item_idwarehouse_id。我已经在3个表上设置了@OneToMany@ManyToOne的关系。此外,我使用软删除,所以每个实体都有@Where(clause = "deleted_flag = 0")
我写了一个查询来获取所有项目,如下所示:

SELECT i FROM Item i
JOIN FETCH i.itemWarehouses iw
JOIN FETCH iw.warehouse

字符串
JPQL在控制台上转换为以下语句:

select
    // fields
from item item0_ 
inner join item_warehouse itemwareho1_ on item0_.id=itemwareho1_.item_id and ( itemwareho1_.deleted_flag = 0) 
inner join warehouse warehouse2_ on itemwareho1_.warehouse_id=warehouse2_.id 
where ( item0_.deleted_flag = 0)


可以看到第一个inner join和最后一个where子句包含deleted_flag = 0,但第二个inner join没有。为什么?
我的实体定义如下

@Entity
@Table(name = "item")
@Where(clause = "deleted_flag = 0")
class Item

@Entity
@Table(name = "item_warehouse",)
@Where(clause = "deleted_flag = 0")
class ItemWarehouse

@Entity
@Table(name = "warehouse")
@Where(clause = "deleted_flag = 0")
class Warehouse

v1uwarro

v1uwarro1#

我也遇到了这个问题,原来这是一个hibernate6.x的bug,在6.2.0.CR4版本中得到了修复;请参阅Hibernate ticket @Where not consistently applied across association boundaries

相关问题