1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.sql.Blob ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.sql.Types ; 10 import java.util.Map ; 11 12 import org.dom4j.Node; 13 import org.hibernate.EntityMode; 14 import org.hibernate.HibernateException; 15 import org.hibernate.MappingException; 16 import org.hibernate.engine.Mapping; 17 import org.hibernate.engine.SessionFactoryImplementor; 18 import org.hibernate.engine.SessionImplementor; 19 import org.hibernate.lob.BlobImpl; 20 import org.hibernate.lob.SerializableBlob; 21 import org.hibernate.util.ArrayHelper; 22 23 27 public class BlobType extends AbstractType { 28 29 public void set(PreparedStatement st, Object value, int index, SessionImplementor session) 30 throws HibernateException, SQLException { 31 32 if (value==null) { 33 st.setNull(index, Types.BLOB); 34 } 35 else { 36 37 if (value instanceof SerializableBlob) { 38 value = ( (SerializableBlob) value ).getWrappedBlob(); 39 } 40 41 final boolean useInputStream = session.getFactory().getDialect().useInputStreamToInsertBlob() && 42 (value instanceof BlobImpl); 43 44 if ( useInputStream ) { 45 BlobImpl blob = (BlobImpl) value; 46 st.setBinaryStream( index, blob.getBinaryStream(), (int) blob.length() ); 47 } 48 else { 49 st.setBlob(index, (Blob ) value); 50 } 51 52 } 53 54 } 55 56 public Object get(ResultSet rs, String name) throws HibernateException, SQLException { 57 Blob value = rs.getBlob(name); 58 return rs.wasNull() ? null : new SerializableBlob(value); 59 } 60 61 public Class getReturnedClass() { 62 return Blob .class; 63 } 64 65 public boolean isEqual(Object x, Object y, EntityMode entityMode) { 66 return x == y; 67 } 68 69 public int getHashCode(Object x, EntityMode entityMode) { 70 return System.identityHashCode(x); 71 } 72 73 public int compare(Object x, Object y, EntityMode entityMode) { 74 return 0; } 76 77 public String getName() { 78 return "blob"; 79 } 80 81 public Serializable disassemble(Object value, SessionImplementor session, Object owner) 82 throws HibernateException { 83 throw new UnsupportedOperationException ("Blobs are not cacheable"); 84 } 85 86 public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) { 87 return value; 88 } 89 90 public Object fromXMLNode(Node xml, Mapping factory) { 91 throw new UnsupportedOperationException ("todo"); 92 } 93 94 public int getColumnSpan(Mapping mapping) { 95 return 1; 96 } 97 98 public boolean isMutable() { 99 return false; 100 } 101 102 public Object nullSafeGet(ResultSet rs, String name, 103 SessionImplementor session, Object owner) 104 throws HibernateException, SQLException { 105 return get(rs, name); 106 } 107 108 public Object nullSafeGet(ResultSet rs, String [] names, 109 SessionImplementor session, Object owner) 110 throws HibernateException, SQLException { 111 return get( rs, names[0] ); 112 } 113 114 public void nullSafeSet(PreparedStatement st, Object value, int index, 115 boolean[] settable, SessionImplementor session) 116 throws HibernateException, SQLException { 117 if ( settable[0] ) set(st, value, index, session); 118 } 119 120 public void nullSafeSet(PreparedStatement st, Object value, int index, 121 SessionImplementor session) throws HibernateException, SQLException { 122 set(st, value, index, session); 123 } 124 125 public Object replace(Object original, Object target, 126 SessionImplementor session, Object owner, Map copyCache) 127 throws HibernateException { 128 return target; 130 } 131 132 public int[] sqlTypes(Mapping mapping) throws MappingException { 133 return new int[] { Types.BLOB }; 134 } 135 136 public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) { 137 throw new UnsupportedOperationException ("todo"); 138 } 139 140 public String toLoggableString(Object value, SessionFactoryImplementor factory) 141 throws HibernateException { 142 return value==null ? "null" : value.toString(); 143 } 144 145 public boolean[] toColumnNullness(Object value, Mapping mapping) { 146 return value==null ? ArrayHelper.FALSE : ArrayHelper.TRUE; 147 } 148 149 } 150 151 152 153 154 155 156 | Popular Tags |