我有以下3个实体:Item
,Warehouse
和ItemWarehouse
,其中有item_id
和warehouse_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