如果我有这个实体:
@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;
}
最佳答案
据我所知,这不是 JPA 规范的一部分。至少 Hibernates JPA 实现提供了一个自己的注解 @Where
可以使用:
@OneToMany
@Where(clause = "state = 'ALIVE'")
Set<Pet> alivePets
关于java - JPA OneToMany 集合的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9896289/