1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.io.ByteArrayInputStream ; 6 import java.sql.Blob ; 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Types ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 14 import org.dom4j.Node; 15 import org.hibernate.EntityMode; 16 import org.hibernate.HibernateException; 17 import org.hibernate.MappingException; 18 import org.hibernate.lob.BlobImpl; 19 import org.hibernate.engine.Mapping; 20 import org.hibernate.engine.SessionFactoryImplementor; 21 import org.hibernate.engine.SessionImplementor; 22 import org.hibernate.usertype.ParameterizedType; 23 import org.hibernate.util.ReflectHelper; 24 import org.hibernate.util.SerializationHelper; 25 26 29 public class SerializableToBlobType extends AbstractLobType implements ParameterizedType { 30 31 public static final String CLASS_NAME = "classname"; 32 private Class serializableClass; 33 private SerializableType type; 34 35 public int[] sqlTypes(Mapping mapping) throws MappingException { 36 return new int[] { Types.BLOB }; 37 } 38 39 public Class getReturnedClass() { 40 return serializableClass; 41 } 42 43 @Override public boolean isEqual(Object x, Object y, EntityMode entityMode, SessionFactoryImplementor factory) { 44 return type.isEqual(x, y); 45 } 46 47 48 @Override public int getHashCode(Object x, EntityMode entityMode, SessionFactoryImplementor session) { 49 return type.getHashCode(x, null); 50 } 51 52 public Object get(ResultSet rs, String name) throws SQLException { 53 Blob blob = rs.getBlob( name ); 54 if ( rs.wasNull() ) return null; 55 int length = (int) blob.length(); 56 byte[] primaryResult = blob.getBytes( 1, length ); 57 return fromBytes(primaryResult); 58 } 59 60 private static byte[] toBytes(Object object) throws SerializationException { 61 return SerializationHelper.serialize( (Serializable ) object ); 62 } 63 64 private static Object fromBytes( byte[] bytes ) throws SerializationException { 65 return SerializationHelper.deserialize(bytes); 66 } 67 68 public void set(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException { 69 byte[] toSet; 70 toSet = toBytes(value); 71 if ( session.getFactory().getDialect().useInputStreamToInsertBlob() ) { 72 st.setBinaryStream( index, new ByteArrayInputStream ( toSet ), toSet.length ); 73 } 74 else { 75 st.setBlob( index, new BlobImpl(toSet) ); 76 } 77 } 78 79 public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException { 80 type.setToXMLNode( node, value, factory ); 81 } 82 83 public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { 84 return type.toLoggableString( value, factory ); 85 } 86 87 public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { 88 return type.fromXMLNode( xml, factory ); 89 } 90 91 public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) 92 throws HibernateException { 93 return type.deepCopy(value, null, null); 94 } 95 96 public boolean isMutable() { 97 return type.isMutable(); 98 } 99 100 public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache) 101 throws HibernateException { 102 return type.replace( original, target, session, owner, copyCache); 103 } 104 105 public boolean[] toColumnNullness(Object value, Mapping mapping) { 106 return type.toColumnNullness( value, mapping); 107 } 108 109 public void setParameterValues(Properties parameters) { 110 if( parameters != null ) { 111 String className = parameters.getProperty(CLASS_NAME); 112 if (className == null) throw new MappingException("No class name defined for type: " + SerializableToBlobType.class.getName() ); 113 try { 114 serializableClass = ReflectHelper.classForName(className); 115 } 116 catch (ClassNotFoundException e) { 117 throw new MappingException("Unable to load class from " + CLASS_NAME + " parameter", e); 118 } 119 } 120 type = new SerializableType(serializableClass); 121 } 122 } 123 | Popular Tags |