blob blob - 1 year ago 171
Java Question

Mapping Queue collections in JPA Hibernate

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

@OneToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE},

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download