4

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

Dhananjay
  • 3,903
  • 2
  • 29
  • 44
  • 1
    Did you look this http://stackoverflow.com/questions/8169196/illegal-attempt-to-map-a-non-collection-as-a-onetomany-manytomany-or-collec . – mbaydar Apr 11 '12 at 11:34
  • Why do you want a queue in a JPA object? I'm surprised that Hibernate wouldn't support Deque as it's an extension of Collection, but you'd almost certainly be able to do whatever it is you want to do with a List instead. – Vala Apr 11 '12 at 11:37
  • @mbaydar The answer there seems to suggest it's because he's not using an interface, that's not the case here - Deque is an interface extending Collection, Iterable, and Queue. – Vala Apr 11 '12 at 11:39
  • http://www.javalobby.org/java/forums/m91832311.html is a good example of how to make your own collection type available for Hibernate. Most likely you could adapt that to use with Deque. – Vala Apr 11 '12 at 11:48

2 Answers2

8

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).

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
0

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;
LanceP
  • 974
  • 1
  • 9
  • 15