I have an Apache Collections HashBag with a @OneToMany relation. I created a class "PersistentHashBagType" which implements "UserCollectionType" to try using type HashBag directly without transforming it to type List, but I have an exception because Hibernate do not detect the HashBag like Collection for use @OneToMany @ManyToMany or @CollectionOfElements. Is that ok? How can I use the HashBag with Hibernate? I referer the class in the entity of my HashBag with the annotation: {{@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @org.hibernate.annotations.CollectionType(type="com.serviceapi.model.tools.PersistentHashBagType") public HashBag<Profesor> getHashBagProfesores() { return hashBagProfesores; } }} And this is the implementation of PersistentHashBagType: {{ package com.serviceapi.model.tools; import java.util.Iterator; import java.util.Map; import org.apache.commons.collections4.bag.HashBag; import org.hibernate.HibernateException; import org.hibernate.collection.internal.PersistentBag; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.usertype.UserCollectionType; public class PersistentHashBagType implements UserCollectionType { @Override public PersistentCollection instantiate(SharedSessionContractImplementor session, CollectionPersister persister) throws HibernateException { return new PersistentBag(session); } @Override public PersistentCollection wrap(SharedSessionContractImplementor session, Object collection) { return new PersistentBag(session, (HashBag<?>)collection); } @Override public Iterator<?> getElementsIterator(Object collection) { return ((HashBag<?>) collection).iterator(); } @Override public boolean contains(Object collection, Object entity) { return ((HashBag<?>) collection).contains(entity); } @Override public Object indexOf(Object collection, Object entity) { throw new UnsupportedOperationException(); } @Override public Object replaceElements(Object original, Object target, CollectionPersister persister, Object owner, Map copyCache, SharedSessionContractImplementor session) throws HibernateException { ((HashBag<?>) target).clear(); ((HashBag) target).addAll((HashBag) original); return target; } @Override public Object instantiate(int anticipatedSize) { return new HashBag<>(); } }}} |