2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
25
package org.hibernate.type;
27
import java.io.Serializable;
28
import java.util.Iterator;
31
import org.hibernate.HibernateException;
32
import org.hibernate.MappingException;
33
import org.hibernate.collection.PersistentCollection;
34
import org.hibernate.engine.SessionImplementor;
35
import org.hibernate.engine.SessionFactoryImplementor;
36
import org.hibernate.persister.collection.CollectionPersister;
37
import org.hibernate.usertype.UserCollectionType;
38
import org.hibernate.usertype.LoggableUserType;
41
* A custom type for mapping user-written classes that implement <tt>PersistentCollection</tt>
43
* @see org.hibernate.collection.PersistentCollection
44
* @see org.hibernate.usertype.UserCollectionType
47
public class CustomCollectionType extends CollectionType {
49
private final UserCollectionType userType;
50
private final boolean customLogging;
52
public CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML) {
53
super(role, foreignKeyPropertyName, isEmbeddedInXML);
55
if ( !UserCollectionType.class.isAssignableFrom( userTypeClass ) ) {
56
throw new MappingException( "Custom type does not implement UserCollectionType: " + userTypeClass.getName() );
60
userType = ( UserCollectionType ) userTypeClass.newInstance();
62
catch ( InstantiationException ie ) {
63
throw new MappingException( "Cannot instantiate custom type: " + userTypeClass.getName() );
65
catch ( IllegalAccessException iae ) {
66
throw new MappingException( "IllegalAccessException trying to instantiate custom type: " + userTypeClass.getName() );
69
customLogging = LoggableUserType.class.isAssignableFrom( userTypeClass );
72
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key)
73
throws HibernateException {
74
return userType.instantiate(session, persister);
77
public PersistentCollection wrap(SessionImplementor session, Object collection) {
78
return userType.wrap(session, collection);
81
public Class getReturnedClass() {
82
return userType.instantiate( -1 ).getClass();
85
public Object instantiate(int anticipatedType) {
86
return userType.instantiate( anticipatedType );
89
public Iterator getElementsIterator(Object collection) {
90
return userType.getElementsIterator(collection);
92
public boolean contains(Object collection, Object entity, SessionImplementor session) {
93
return userType.contains(collection, entity);
95
public Object indexOf(Object collection, Object entity) {
96
return userType.indexOf(collection, entity);
99
public Object replaceElements(Object original, Object target, Object owner, Map copyCache, SessionImplementor session)
100
throws HibernateException {
101
CollectionPersister cp = session.getFactory().getCollectionPersister( getRole() );
102
return userType.replaceElements(original, target, cp, owner, copyCache, session);
105
protected String renderLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException {
106
if ( customLogging ) {
107
return ( ( LoggableUserType ) userType ).toLoggableString( value, factory );
110
return super.renderLoggableString( value, factory );
114
public UserCollectionType getUserType() {