How to specify the cardinality of a @OneToMany in EclipseLink/JPA

邮差的信 提交于 2019-12-06 11:26:11

问题


I'm trying to impose a @Oneto7 association. I'd have imagined an attribute that specifies the target many value, but have found none. If there is no such attribute, how else, in JPA/EclipseLink would one achieve it?


回答1:


You could use the Bean Validation API (JSR-303) - Hibernate Validator being the RI - and add a Size constraint on your collection:

@Size(min = 7, max = 7) protected Set<Foo> foos = new HashSet<Foo>();

If you're using JPA 1.0, have a look at this previous answer to see how to use Bean Validation with JPA 1.0.




回答2:


You may use the Oval library and set e.g.:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "whatevery")
@Size(max = 30)
private List<SuperDocument> documents;

Here is the documentation for further validations of Oval: http://oval.sourceforge.net/userguide.html#api-documentation



来源:https://stackoverflow.com/questions/2705759/how-to-specify-the-cardinality-of-a-onetomany-in-eclipselink-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!