java JPA OneToMany 集合的条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9896289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:48:02 来源:igfitidea点击:
Conditions for JPA OneToMany collection
提问by Bart van Heukelom
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;
}
回答by magomi
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:
据我所知,这不是 JPA 规范的一部分。至少 Hibernates JPA 实现提供了一个@Where
可以使用的自己的注释:
@OneToMany
@Where(clause = "state = 'ALIVE'")
Set<Pet> alivePets