Mapping Queue collections in JPA Hibernate

情到浓时终转凉″ 提交于 2019-12-22 10:22:40

问题


Is it possible to have following collection mapping in JPA / hibernate

@OneToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE},
fetch=FetchType.LAZY,mappedBy="parent")

private Deque<Child> childrens;

It throws error

Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements

I am using JPA 2.0 with Hibernate 3


回答1:


No, JPA does not support Deque. In JPA 2.0 specification this is explained following way:

Collection-valued persistent fields and properties must be defined in terms of one of the following collection-valued interfaces regardless of whether the entity class otherwise adheres to the JavaBeans method conventions noted above and whether field or property access is used: java.util.Collection, java.util.Set, java.util.List[3], java.util.Map. The collection implementa- tion type may be used by the application to initialize fields or properties before the entity is made persistent. Once the entity becomes managed (or detached), subsequent access must be through the interface type.

I would suggest to add to entity methods that provide needed Deque functionality (or expose view as Deque to persisted list). Other possibility is custom collection as suggested in comments (Thor84no).




回答2:


While JPA does not support Deque as mentioned by Mikko, you could simply update your code to be an ArrayDeque and you should be good to go.

@OneToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE},
fetch=FetchType.LAZY,mappedBy="parent")

private ArrayDeque<Child> childrens;


来源:https://stackoverflow.com/questions/10104946/mapping-queue-collections-in-jpa-hibernate

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