Hibernate persistence class: @Entity public class A {

    @OneToMany(mappedBy = "a")
    private Set<B> bSet = new HashSet<B>();

    private boolean DELETED;


Class B also have a DELETED property. How can we process DELETED property during join automatically, for select only not deleted entities. May be with help of some annotations. It is possible?

Was it helpful?

Solution 2

Many thx! that is my resolve:

public class A extends DeletableEntity{

    @OneToMany(mappedBy = "a")
    @Where(clause = "DELETED=0 or DELETED is null")
    private Set<B> bSet = new HashSet<B>();

    public Set<B> getBSet() {
        return bSet;

    public void setBSet(Set<B> bSet) {
        this.bSet = bSet;

and HQL "select a.DELETED from A a join a.bSet bSet" will be generate native SQL like this:

select a0_.DELETED as col_0_0_ from A a0_ inner join B bset1_ on and ( bset1_.DELETED=0 or bset1_.DELETED is null)

and entityManager.find(...) will be working right :)


According to Hibernate documentation you should use ResultTransformer (ALIAS_TO_ENTITY_MAP).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top