JOIN FETCH中缺少Hibernate @Where子句

时间:2018-09-28 18:48:06

标签: sql hibernate jpql

我有以下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

0 个答案:

没有答案