@EntityGraph unloads related entities ignoring @Where annotation

Viewed 86

I am using soft delete in my project. I need to unload a lot of related entities. I am using @EntityGraph with multiple entities at the same time - entityA.EntityB.EntityC, this does not work on related entities. Each entity has the @Where annotation. I've tried using @Where and @WhereJoinColumn entities on the field. It gave no results

@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);

I want Hibernate to return me a query like

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

But it comes back to me

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

How do I get Hibernate to see all my @Where annotations when unloading many related entities. I wrote a query in hql, but it is too cumbersome. Thanks

0 Answers
Related