Good day,
i have trouble with preparing LinkedHashMap with using UserCollectionType. I found some material on this forum and with google and after it, i have tried to implement LinkedHashMap, but i wasn´t sucessfull.
I don´t have problem with exception, but my LinkedHashMap work wrong. It can´t make index for each element. Rather for none element and it look like same like hashmap. Have anyone some idea, how to do it? Mainly how to implement indexing. Or is this bad way to implement LinkedHashMap?
This is my source code:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.collection.PersistentMap;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.usertype.UserCollectionType;
/**
*
* @author petr
*/
public class LinkedHashMapP implements UserCollectionType {
public boolean contains(Object collection, Object obj) {
LinkedHashMap lhm = (LinkedHashMap) collection;
return lhm.containsValue(obj);
}
public Iterator getElementsIterator(Object collection) {
return ((Map) collection).values().iterator();
}
public Object indexOf(Object collection, Object entity) {
List l = new ArrayList();
l.addAll(((LinkedHashMap) collection).values());
int index = l.indexOf(entity);
if (index < 0) {
return null;
} else {
return new Integer(index);
}
}
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister arg1) throws HibernateException {
return new PersistentMap(session);
}
public Object instantiate(int arg0) {
return new LinkedHashMap();
}
public Object replaceElements(Object collectionA, Object collectionB, CollectionPersister arg2, Object arg3, Map arg4, SessionImplementor arg5) throws HibernateException {
LinkedHashMap mapA = (LinkedHashMap) collectionA;
LinkedHashMap mapB = (LinkedHashMap) collectionB;
mapB.clear();
mapB.putAll(mapA);
return mapB;
}
public PersistentCollection wrap(SessionImplementor session, Object collection) {
return new PersistentMap(session, (LinkedHashMap) collection);
}
}
Hibernate version:3.2.5
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="pokus.PokusMap" table="PokusMap">
<id name="id" column="Id">
<generator class="increment"/>
</id>
<map name="hm" table="hashmapa">
<key column="id"/>
<map-key column="cislo" type="int"/>
<element column="text" type="string"/>
</map>
<map name="lhm" table="linkedhashmapa" collection-type="pokus.LinkedHashMapP">
<key column="id"/>
<map-key column="cislo" type="int"/>
<element column="text" type="string"/>
</map>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
It isn´t so important, work good.
Full stack trace of any exception that occurs:
None exception
Name and version of the database you are using:
MySQL 5
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Thanks for Your reply Petr Janura
|