@Where clause whith nested association

36 views Asked by At

I have the following relationship:

@Entity
public class User {

   //other fields

   @Where(clause = "media.deleted = false")
   @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
   private List<Favorite> favorites = new ArrayList<>();

}

@Entity
public class Favorite {

    //other fields

    @ManyToOne(optional = false)
    @JoinColumn(name = "mediaId", nullable = false)
    private Media media;

    @CreatedBy
    @ManyToOne(optional = false)
    @JoinColumn(name = "userId", nullable = false)
    private User user;
}

@Entity
public class Media {

    //other fields

    @Column(columnDefinition = "bool default false")
    private boolean deleted;
}

With @Where(clause = "media.deleted = false") hibernate generates following query:

select
        f1_0.userId,
        ...
        m1_0.xxx,    
        u1_0.viewsCount,
        m1_0.viewsCount 
    from
        favorites f1_0 
    left join
        medias m1_0 
            on m1_0.id=f1_0.mediaId 
    left join
        users u1_0 
            on u1_0.id=m1_0.userId 
    where
        f1_0.userId=? 
        and (
            **media.deleted = 0**
        ) 

Resulting in SQL Error: Unknown column 'media.deleted' in 'where clause'

When i change the clause to m1_0.deleted = false it works!

But i suspect the generated alias to not be fixed and change depending on DB or OS.

So How to infer the generated hibernate table alias name in the @Where clause?

0

There are 0 answers