Jpa OneToMany with condition

jpa, jpa-2.0, spring-data-jpa

Solution

Try to use Hibernate `@Where` annotation, for example:

@Entity
public class Person {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    @Enumerated(EnumType.STRING)
    private Gender gender;

    @ManyToOne
    private Person parent;

    @Where(clause = "gender = 'MALE'")
    @OneToMany(mappedBy = "person")
    private List<Person> sons;

    @Where(clause = "gender = 'FEMALE'")
    @OneToMany(mappedBy = "person")
    private List<Person> daughters;
}

public enum Gender {
   MALE, FEMALE
}

Problem

I have 2 tables: The first is "Persons": - person_id, - person_name The second is "PersonsGraphs": - person_id1, - person_id2, - relation_type I'm looking for a way to build a "family tree". My first option is: load personGraphs into a HashTable and then recursively build the tree. The second option I have come up with: use `@OneToMany jpa-relation`. This can work, but sometimes I have some `relation_types` that I want/don't want to include. Are there any options that would allow me to set some condition on the `@OneToMany` relation while using `@JoinTable`? Thanks! Oak

Original source