1 package org.hibernate.type; 3 4 import org.hibernate.usertype.UserType; 5 import org.hibernate.HibernateException; 6 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.sql.PreparedStatement ; 10 import java.sql.Types ; 11 import java.io.Serializable ; 12 import java.io.Reader ; 13 import java.io.IOException ; 14 import java.io.StringReader ; 15 16 21 public class StringClobType implements UserType { 22 public int[] sqlTypes() { 23 return new int[] { Types.CLOB }; 24 } 25 26 public Class returnedClass() { 27 return String .class; 28 } 29 30 public boolean equals(Object x, Object y) throws HibernateException { 31 return ( x == y) || ( x != null && x.equals(y) ); 32 } 33 34 public int hashCode(Object x) throws HibernateException { 35 return x.hashCode(); 36 } 37 38 public Object nullSafeGet(ResultSet rs, String [] names, Object owner) throws HibernateException, SQLException { 39 Reader reader = rs.getCharacterStream( names[0] ); 40 if (reader == null) return null; 41 StringBuffer result = new StringBuffer (); 42 try { 43 char[] charbuf = new char[4096]; 44 for (int i = reader.read(charbuf) ; i > 0 ; i = reader.read(charbuf) ) { 45 result.append(charbuf, 0, i); 46 } 47 } catch (IOException e) { 48 throw new SQLException ( e.getMessage() ); 49 } 50 return result.toString(); 51 } 52 53 public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { 54 if (value != null) { 55 String string = (String ) value; 56 StringReader reader = new StringReader ( string ); 57 st.setCharacterStream( index, reader, string.length() ); 58 } 59 else { 60 st.setNull( index, sqlTypes()[0] ); 61 } 62 } 63 64 public Object deepCopy(Object value) throws HibernateException { 65 return value; 67 } 68 69 public boolean isMutable() { 70 return false; 71 } 72 73 public Serializable disassemble(Object value) throws HibernateException { 74 return (Serializable ) value; 75 } 76 77 public Object assemble(Serializable cached, Object owner) throws HibernateException { 78 return cached; 79 } 80 81 public Object replace(Object original, Object target, Object owner) throws HibernateException { 82 return original; 83 } 84 } 85 | Popular Tags |