1 package org.hibernate.collection; 3 4 import java.io.Serializable ; 5 import java.util.List ; 6 7 import org.dom4j.Element; 8 import org.hibernate.HibernateException; 9 import org.hibernate.engine.SessionImplementor; 10 import org.hibernate.persister.collection.CollectionPersister; 11 import org.hibernate.type.NullableType; 12 import org.hibernate.type.Type; 13 14 17 public class PersistentMapElementHolder extends PersistentIndexedElementHolder { 18 19 public PersistentMapElementHolder(SessionImplementor session, Element element) { 20 super( session, element ); 21 } 22 23 public PersistentMapElementHolder(SessionImplementor session, CollectionPersister persister, 24 Serializable key) throws HibernateException { 25 super( session, persister, key ); 26 } 27 28 public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner) 29 throws HibernateException { 30 31 Type elementType = persister.getElementType(); 32 Type indexType = persister.getIndexType(); 33 final String indexNodeName = getIndexAttributeName(persister); 34 35 Serializable [] cached = (Serializable []) disassembled; 36 37 for ( int i=0; i<cached.length; ) { 38 Object index = indexType.assemble( cached[i++], getSession(), owner ); 39 Object object = elementType.assemble( cached[i++], getSession(), owner ); 40 41 Element subelement = element.addElement( persister.getElementNodeName() ); 42 elementType.setToXMLNode( subelement, object, persister.getFactory() ); 43 44 String indexString = ( (NullableType) indexType ).toXMLString( index, persister.getFactory() ); 45 setIndex( subelement, indexNodeName, indexString ); 46 } 47 48 } 49 50 public Serializable disassemble(CollectionPersister persister) throws HibernateException { 51 52 Type elementType = persister.getElementType(); 53 Type indexType = persister.getIndexType(); 54 final String indexNodeName = getIndexAttributeName(persister); 55 56 List elements = element.elements( persister.getElementNodeName() ); 57 int length = elements.size(); 58 Serializable [] result = new Serializable [length*2]; 59 for ( int i=0; i<length*2; ) { 60 Element elem = (Element) elements.get(i/2); 61 Object object = elementType.fromXMLNode( elem, persister.getFactory() ); 62 final String indexString = getIndex(elem, indexNodeName, i); 63 Object index = ( (NullableType) indexType ).fromXMLString( indexString, persister.getFactory() ); 64 result[i++] = indexType.disassemble( index, getSession(), null ); 65 result[i++] = elementType.disassemble( object, getSession(), null ); 66 } 67 return result; 68 } 69 70 71 } 72 | Popular Tags |