{quote}Hibernate uses its own collection implementations which are enriched with lazy-loading, caching or state change detection semantics. For this reason, persistent collections must be declared as an interface type. The actual interface might be java.util.Collection, java.util.List, java.util.Set, java.util.Map, java.util.SortedSet, java.util.SortedMap or even other object types (meaning you will have to write an implementation of org.hibernate.usertype.UserCollectionType).{quote}
[Hibernate 5.2 User Guide section 2.8|http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#collections]
{quote}The reason why the Queue interface is not used for the entity attribute is because Hibernate only allows the following types:
java.util.List
java.util.Set
java.util.Map
java.util.SortedSet
java.util.SortedMap {quote}
"Example 192. Custom collection mapping example" omits an example of getters and setters.
[Hibernate 5.2 User Guide section 2.8.12|http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#collections-custom]
It is possible to use alternative interfaces (e.g. Queue) for the getter/setter provided annotations are on the field rather than the methods. For instance, the following should work and it would be helpful to extend the example.
{code} @Entity(name = "Person") public static class Person { ... @OneToMany(cascade = CascadeType.ALL) @CollectionType( type = "org.hibernate.userguide.collections.type.QueueType") private Collection<Phone> phones = new Queue<Phone>();
// Supports Queue<Phone> phones = person.getPhones(); public Queue<Phone> getPhones() { return (Queue<Phone>) phones; } } {code}
If the getter is annotated, it must be marked with one of the types mentioned above but can be cast by code using the entity. This could also be more clearly called out:
{code} @Entity(name = "Person") public static class Person { ... private Queue<Phone> phones = new Queue<Phone> ( );
// Requires Queue<Phone> phones = (Queue<Phone> ) person.getPhones(); @OneToMany(cascade = CascadeType.ALL) @CollectionType( type = "org.hibernate.userguide.collections.type.QueueType") public Collection<Phone> getPhones() { return (Queue<Phone>) phones; } } { code} |
|