如果我有这个实体:
@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;
}
答案 0 :(得分:26)
据我所知,这不是JPA规范的一部分。至少Hibernates JPA实现提供了一个可以使用的自己的注释@Where
:
@OneToMany
@Where(clause = "state = 'ALIVE'")
Set<Pet> alivePets