views:

347

answers:

2

Hibernate persistence class: @Entity public class A {

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

    @Basic
    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?

+1  A: 

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

Tadeusz Kopec
A: 

Many thx! that is my resolve:

@Entity
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 a0_.id=bset1_.a_id and ( bset1_.DELETED=0 or bset1_.DELETED is null)

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

Max