KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > CustomCollectionType


1 //$Id: CustomCollectionType.java,v 1.12 2005/07/05 17:42:23 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
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 /**
16  * A custom type for mapping user-written classes that implement <tt>PersistentCollection</tt>
17  *
18  * @see org.hibernate.collection.PersistentCollection
19  * @see org.hibernate.usertype.UserCollectionType
20  * @author Gavin King
21  */

22 public class CustomCollectionType extends CollectionType {
23
24     private final UserCollectionType userType;
25
26     public CustomCollectionType(Class JavaDoc userTypeClass, String JavaDoc role, String JavaDoc 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 JavaDoc ie) {
37             throw new MappingException( "Cannot instantiate custom type: " + userTypeClass.getName() );
38         }
39         catch (IllegalAccessException JavaDoc 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 JavaDoc key)
46     throws HibernateException {
47         return userType.instantiate(session, persister);
48     }
49
50     public PersistentCollection wrap(SessionImplementor session, Object JavaDoc collection) {
51         return userType.wrap(session, collection);
52     }
53
54     public Class JavaDoc getReturnedClass() {
55         return userType.instantiate().getClass();
56     }
57
58     public Object JavaDoc instantiate(Object JavaDoc original) {
59         return userType.instantiate();
60     }
61
62     public Iterator JavaDoc getElementsIterator(Object JavaDoc collection) {
63         return userType.getElementsIterator(collection);
64     }
65     public boolean contains(Object JavaDoc collection, Object JavaDoc entity, CollectionPersister persister, SessionImplementor session) {
66         return userType.contains(collection, entity);
67     }
68     public Object JavaDoc indexOf(Object JavaDoc collection, Object JavaDoc entity) {
69         return userType.indexOf(collection, entity);
70     }
71     
72     public Object JavaDoc replaceElements(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner, Map JavaDoc 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