1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import org.hibernate.HibernateException; 9 import org.hibernate.MappingException; 10 import org.hibernate.collection.PersistentCollection; 11 import org.hibernate.engine.SessionImplementor; 12 import org.hibernate.persister.collection.CollectionPersister; 13 import org.hibernate.usertype.UserCollectionType; 14 15 22 public class CustomCollectionType extends CollectionType { 23 24 private final UserCollectionType userType; 25 26 public CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML) { 27 super(role, foreignKeyPropertyName, isEmbeddedInXML); 28 29 if ( !UserCollectionType.class.isAssignableFrom(userTypeClass) ) { 30 throw new MappingException( "Custom type does not implement UserCollectionType: " + userTypeClass.getName() ); 31 } 32 33 try { 34 userType = (UserCollectionType) userTypeClass.newInstance(); 35 } 36 catch (InstantiationException ie) { 37 throw new MappingException( "Cannot instantiate custom type: " + userTypeClass.getName() ); 38 } 39 catch (IllegalAccessException iae) { 40 throw new MappingException( "IllegalAccessException trying to instantiate custom type: " + userTypeClass.getName() ); 41 } 42 43 } 44 45 public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) 46 throws HibernateException { 47 return userType.instantiate(session, persister); 48 } 49 50 public PersistentCollection wrap(SessionImplementor session, Object collection) { 51 return userType.wrap(session, collection); 52 } 53 54 public Class getReturnedClass() { 55 return userType.instantiate().getClass(); 56 } 57 58 public Object instantiate(Object original) { 59 return userType.instantiate(); 60 } 61 62 public Iterator getElementsIterator(Object collection) { 63 return userType.getElementsIterator(collection); 64 } 65 public boolean contains(Object collection, Object entity, CollectionPersister persister, SessionImplementor session) { 66 return userType.contains(collection, entity); 67 } 68 public Object indexOf(Object collection, Object entity) { 69 return userType.indexOf(collection, entity); 70 } 71 72 public Object replaceElements(Object original, Object target, Object owner, Map copyCache, SessionImplementor session) 73 throws HibernateException { 74 CollectionPersister cp = session.getFactory().getCollectionPersister( getRole() ); 75 return userType.replaceElements(original, target, cp, owner, copyCache, session); 76 } 77 } 78 | Popular Tags |