简体   繁体   中英

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;

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM