JPA OneToMany系列的条件

时间:2012-03-27 19:26:42

标签: java hibernate jpa

如果我有这个实体:

@Entity
class Pet {

    @Id
    long id;

    public enum State { ALIVE, DEAD }

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

    @...
    String name;

}

我可以创建这样的映射:

@Entity
class Owner {

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

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

}

1 个答案:

答案 0 :(得分:26)

据我所知,这不是JPA规范的一部分。至少Hibernates JPA实现提供了一个可以使用的自己的注释@Where

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