We're updating the issue view to help you get more done. 

Save a HashBag implementing UserCollectionType

Description

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:

1 2 3 4 5 @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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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<>(); } }

Environment

Spring Boot Data JPA 2.1.1

Status

Assignee

Unassigned

Reporter

Raquel Fernández González

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Feedback Requested

2019/02/21

Feedback Requested By

Gail Badner

Affects versions

5.3.7

Priority

Minor