Bart van Heukelom
Bart van Heukelom

Reputation: 44114

Conditions for JPA OneToMany collection

If I have this entity:

@Entity
class Pet {

    @Id
    long id;

    public enum State { ALIVE, DEAD }

    @Enumerated(EnumType.STRING)
    @...
    State state;

    @...
    String name;

}

Can I create a mapping like this:

@Entity
class Owner {

    @OneToMany(condition="state = ALIVE") // or something like that
    Set<Pet> alivePets;

    @OneToMany(condition="state = DEAD")
    Set<Pet> deadPets;

}

Upvotes: 36

Views: 31158

Answers (1)

magomi
magomi

Reputation: 6685

As far as I know this is not part of the JPA spec. At least Hibernates JPA implementation provides an own annotation @Where which can be used:

@OneToMany
@Where(clause = "state = 'ALIVE'")
Set<Pet> alivePets

Upvotes: 52

Related Questions