1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.lang.reflect.Method ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.util.Map ; 10 import java.util.Properties ; 11 12 import org.dom4j.Element; 13 import org.dom4j.Node; 14 import org.hibernate.EntityMode; 15 import org.hibernate.FetchMode; 16 import org.hibernate.HibernateException; 17 import org.hibernate.MappingException; 18 import org.hibernate.engine.CascadeStyle; 19 import org.hibernate.engine.Mapping; 20 import org.hibernate.engine.SessionFactoryImplementor; 21 import org.hibernate.engine.SessionImplementor; 22 import org.hibernate.usertype.CompositeUserType; 23 24 28 public class CompositeCustomType extends AbstractType 29 implements AbstractComponentType { 30 31 private final CompositeUserType userType; 32 private final String name; 33 34 public CompositeCustomType(Class userTypeClass, Properties parameters) 35 throws MappingException { 36 name = userTypeClass.getName(); 37 38 if ( !CompositeUserType.class.isAssignableFrom(userTypeClass) ) { 39 throw new MappingException( 40 "Custom type does not implement CompositeUserType: " + 41 userTypeClass.getName() 42 ); 43 } 44 45 try { 46 userType = (CompositeUserType) userTypeClass.newInstance(); 47 } 48 catch (InstantiationException ie) { 49 throw new MappingException( 50 "Cannot instantiate custom type: " + 51 userTypeClass.getName() 52 ); 53 } 54 catch (IllegalAccessException iae) { 55 throw new MappingException( 56 "IllegalAccessException trying to instantiate custom type: " + 57 userTypeClass.getName() 58 ); 59 } 60 TypeFactory.injectParameters(userType, parameters); 61 } 62 63 public boolean isMethodOf(Method method) { 64 return false; 65 } 66 67 public Type[] getSubtypes() { 68 return userType.getPropertyTypes(); 69 } 70 71 public String [] getPropertyNames() { 72 return userType.getPropertyNames(); 73 } 74 75 public Object [] getPropertyValues(Object component, SessionImplementor session) 76 throws HibernateException { 77 return getPropertyValues( component, session.getEntityMode() ); 78 } 79 80 public Object [] getPropertyValues(Object component, EntityMode entityMode) 81 throws HibernateException { 82 83 int len = getSubtypes().length; 84 Object [] result = new Object [len]; 85 for ( int i=0; i<len; i++ ) { 86 result[i] = getPropertyValue(component, i); 87 } 88 return result; 89 } 90 91 public void setPropertyValues(Object component, Object [] values, EntityMode entityMode) 92 throws HibernateException { 93 94 for (int i=0; i<values.length; i++) { 95 userType.setPropertyValue( component, i, values[i] ); 96 } 97 } 98 99 public Object getPropertyValue(Object component, int i, SessionImplementor session) 100 throws HibernateException { 101 return getPropertyValue(component, i); 102 } 103 104 public Object getPropertyValue(Object component, int i) 105 throws HibernateException { 106 return userType.getPropertyValue(component, i); 107 } 108 109 public CascadeStyle getCascadeStyle(int i) { 110 return CascadeStyle.NONE; 111 } 112 113 public FetchMode getFetchMode(int i) { 114 return FetchMode.DEFAULT; 115 } 116 117 public boolean isComponentType() { 118 return true; 119 } 120 121 public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) 122 throws HibernateException { 123 return userType.deepCopy(value); 124 } 125 126 public Object assemble( 127 Serializable cached, 128 SessionImplementor session, 129 Object owner) 130 throws HibernateException { 131 132 return userType.assemble(cached, session, owner); 133 } 134 135 public Serializable disassemble(Object value, SessionImplementor session, Object owner) 136 throws HibernateException { 137 return userType.disassemble(value, session); 138 } 139 140 public Object replace( 141 Object original, 142 Object target, 143 SessionImplementor session, 144 Object owner, 145 Map copyCache) 146 throws HibernateException { 147 return userType.replace(original, target, session, owner); 148 } 149 150 public boolean isEqual(Object x, Object y, EntityMode entityMode) 151 throws HibernateException { 152 return userType.equals(x, y); 153 } 154 155 public int getHashCode(Object x, EntityMode entityMode) { 156 return userType.hashCode(x); 157 } 158 159 public int getColumnSpan(Mapping mapping) throws MappingException { 160 Type[] types = userType.getPropertyTypes(); 161 int n=0; 162 for (int i=0; i<types.length; i++) { 163 n+=types[i].getColumnSpan(mapping); 164 } 165 return n; 166 } 167 168 public String getName() { 169 return name; 170 } 171 172 public Class getReturnedClass() { 173 return userType.returnedClass(); 174 } 175 176 public boolean isMutable() { 177 return userType.isMutable(); 178 } 179 180 public Object nullSafeGet( 181 ResultSet rs, 182 String columnName, 183 SessionImplementor session, 184 Object owner) 185 throws HibernateException, SQLException { 186 187 return userType.nullSafeGet(rs, new String [] {columnName}, session, owner); 188 } 189 190 public Object nullSafeGet( 191 ResultSet rs, 192 String [] names, 193 SessionImplementor session, 194 Object owner) 195 throws HibernateException, SQLException { 196 197 return userType.nullSafeGet(rs, names, session, owner); 198 } 199 200 public void nullSafeSet( 201 PreparedStatement st, 202 Object value, 203 int index, 204 SessionImplementor session) 205 throws HibernateException, SQLException { 206 207 userType.nullSafeSet(st, value, index, session); 208 209 } 210 211 public void nullSafeSet( 212 PreparedStatement st, 213 Object value, 214 int index, 215 boolean[] settable, 216 SessionImplementor session) 217 throws HibernateException, SQLException { 218 219 userType.nullSafeSet(st, value, index, session); 220 221 } 222 223 public int[] sqlTypes(Mapping mapping) throws MappingException { 224 Type[] types = userType.getPropertyTypes(); 225 int[] result = new int[ getColumnSpan(mapping) ]; 226 int n=0; 227 for (int i=0; i<types.length; i++) { 228 int[] sqlTypes = types[i].sqlTypes(mapping); 229 for ( int k=0; k<sqlTypes.length; k++ ) result[n++] = sqlTypes[k]; 230 } 231 return result; 232 } 233 234 public String toLoggableString(Object value, SessionFactoryImplementor factory) 235 throws HibernateException { 236 237 return value==null ? "null" : value.toString(); 238 } 239 240 public boolean[] getPropertyNullability() { 241 return null; 242 } 243 244 public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { 245 return xml; 246 } 247 248 public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) 249 throws HibernateException { 250 replaceNode( node, (Element) value ); 251 } 252 253 public boolean[] toColumnNullness(Object value, Mapping mapping) { 254 boolean[] result = new boolean[ getColumnSpan(mapping) ]; 255 if (value==null) return result; 256 Object [] values = getPropertyValues(value, EntityMode.POJO); int loc = 0; 258 Type[] propertyTypes = getSubtypes(); 259 for ( int i=0; i<propertyTypes.length; i++ ) { 260 boolean[] propertyNullness = propertyTypes[i].toColumnNullness( values[i], mapping ); 261 System.arraycopy(propertyNullness, 0, result, loc, propertyNullness.length); 262 loc += propertyNullness.length; 263 } 264 return result; 265 } 266 } 267 | Popular Tags |