首页 > 解决方案 > @EntityGraph 卸载相关实体忽略 @Where 注释

问题描述

我在我的项目中使用软删除。我需要卸载很多相关实体。我同时对多个实体使用@EntityGraph - entityA.EntityB.EntityC,这不适用于相关实体。每个实体都有@Where 注释。我尝试在现场使用 @Where 和 @WhereJoinColumn 实体。它没有给出任何结果

@Entity
@Where(clause = "field is null")
public class EntityA {
    @OneToOne(mappedBy = "entityA", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    @PrimaryKeyJoinColumn
    private EntityB entityB;
@Entity
@Where(clause = "field is null")
public class EntityB {
    @MapsId
    @OneToOne(fetch = FetchType.LAZY)
    private EntityA entityA;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", referencedColumnName = "id", nullable = false)
    private EntityC entityC;
@Entity
@Where(clause = "field is null")
public class EntityC {
    @OneToMany(mappedBy = "EntityB")
    @Audited(targetAuditMode = NOT_AUDITED)
    private Set<EntityB> set = new HashSet<>();
public interface EntityARepository extends JpaRepository<EntityA, Integer> {
    @EntityGraph(attributePaths = {"entityB.entityC"})
    Optional<EntityA> findById(Integer id);

我希望 Hibernate 向我返回一个查询,例如

SELECT * FROM EntityA
LEFT JOIN EntityB ON EntityA.id = EntityB.id
AND EntityB.field is null
LEFT JOIN EntityC ON EntityB.id = EntityC.id
AND EntityC.field is null
WHERE EntityA.id = ? AND EntityA.field is null

但它回到我身边

SELECT * FROM EntityA
LEFT JOIN EntityB ON EntityA.id = EntityB.id
LEFT JOIN EntityC ON EntityB.id = EntityC.id
AND EntityC.field is null
WHERE EntityA.id = ? AND EntityA.field is null

卸载许多相关实体时,如何让 Hibernate 查看我的所有 @Where 注释。我在hql中写了一个查询,但是太麻烦了。谢谢

标签: javaspring-boothibernate

解决方案


推荐阅读