1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.sql.Clob ; 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.Hibernate; 15 import org.hibernate.HibernateException; 16 import org.hibernate.MappingException; 17 import org.hibernate.engine.Mapping; 18 import org.hibernate.engine.SessionFactoryImplementor; 19 import org.hibernate.engine.SessionImplementor; 20 import org.hibernate.lob.ClobImpl; 21 import org.hibernate.lob.SerializableClob; 22 import org.hibernate.util.ArrayHelper; 23 24 28 public class ClobType extends AbstractType { 29 30 public void set(PreparedStatement st, Object value, int index, SessionImplementor session) 31 throws HibernateException, SQLException { 32 33 if (value==null) { 34 st.setNull(index, Types.CLOB); 35 } 36 else { 37 38 if (value instanceof SerializableClob) { 39 value = ( (SerializableClob) value ).getWrappedClob(); 40 } 41 42 final boolean useReader = session.getFactory().getDialect().useInputStreamToInsertBlob() && 43 (value instanceof ClobImpl); 44 45 if ( useReader ) { 46 ClobImpl clob = (ClobImpl) value; 47 st.setCharacterStream( index, clob.getCharacterStream(), (int) clob.length() ); 48 } 49 else { 50 st.setClob(index, (Clob ) value); 51 } 52 53 } 54 55 } 56 57 public Object get(ResultSet rs, String name) throws HibernateException, SQLException { 58 Clob value = rs.getClob(name); 59 return rs.wasNull() ? null : new SerializableClob(value); 60 } 61 62 public Class getReturnedClass() { 63 return Clob .class; 64 } 65 66 public boolean isEqual(Object x, Object y, EntityMode entityMode) { 67 return x == y; 68 } 69 70 public int getHashCode(Object x, EntityMode entityMode) { 71 return System.identityHashCode(x); 72 } 73 74 public int compare(Object x, Object y, EntityMode entityMode) { 75 return 0; } 77 78 public String getName() { 79 return "clob"; 80 } 81 82 public Serializable disassemble(Object value, SessionImplementor session, Object owner) 83 throws HibernateException { 84 throw new UnsupportedOperationException ("Clobs are not cacheable"); 85 } 86 87 public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) { 88 return value; 89 } 90 91 public Object fromXMLNode(Node xml, Mapping factory) { 92 return Hibernate.createClob( xml.getText() ); 93 } 94 95 public int getColumnSpan(Mapping mapping) { 96 return 1; 97 } 98 99 public boolean isMutable() { 100 return false; 101 } 102 103 public Object nullSafeGet(ResultSet rs, String name, 104 SessionImplementor session, Object owner) 105 throws HibernateException, SQLException { 106 return get(rs, name); 107 } 108 109 public Object nullSafeGet(ResultSet rs, String [] names, 110 SessionImplementor session, Object owner) 111 throws HibernateException, SQLException { 112 return get( rs, names[0] ); 113 } 114 115 public void nullSafeSet(PreparedStatement st, Object value, int index, 116 boolean[] settable, SessionImplementor session) 117 throws HibernateException, SQLException { 118 if ( settable[0] ) set(st, value, index, session); 119 } 120 121 public void nullSafeSet(PreparedStatement st, Object value, int index, 122 SessionImplementor session) throws HibernateException, SQLException { 123 set(st, value, index, session); 124 } 125 126 public Object replace(Object original, Object target, 127 SessionImplementor session, Object owner, Map copyCache) 128 throws HibernateException { 129 return target; 131 } 132 133 public int[] sqlTypes(Mapping mapping) throws MappingException { 134 return new int[] { Types.CLOB }; 135 } 136 137 public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) { 138 if (value!=null) { 139 Clob clob = (Clob ) value; 140 try { 141 int len = (int) clob.length(); 142 node.setText( clob.getSubString(0, len) ); 143 } 144 catch (SQLException sqle) { 145 throw new HibernateException("could not read XML from Clob", sqle); 146 } 147 } 148 } 149 150 public String toLoggableString(Object value, SessionFactoryImplementor factory) 151 throws HibernateException { 152 return value==null ? "null" : value.toString(); 153 } 154 155 public boolean[] toColumnNullness(Object value, Mapping mapping) { 156 return value==null ? ArrayHelper.FALSE : ArrayHelper.TRUE; 157 } 158 159 } 160 161 162 163 164 165 166 | Popular Tags |