KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > StringClobType


1 //$Id: StringClobType.java,v 1.1 2005/04/03 23:37:29 epbernard Exp $
2
package org.hibernate.type;
3
4 import org.hibernate.usertype.UserType;
5 import org.hibernate.HibernateException;
6
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.Types JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.io.Reader JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.StringReader JavaDoc;
15
16 /**
17  * Map a String to a Clob
18  *
19  * @author Emmanuel Bernard
20  */

21 public class StringClobType implements UserType {
22     public int[] sqlTypes() {
23         return new int[] { Types.CLOB };
24     }
25
26     public Class JavaDoc returnedClass() {
27         return String JavaDoc.class;
28     }
29
30     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
31         return ( x == y) || ( x != null && x.equals(y) );
32     }
33
34     public int hashCode(Object JavaDoc x) throws HibernateException {
35         return x.hashCode();
36     }
37
38     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
39         Reader JavaDoc reader = rs.getCharacterStream( names[0] );
40         if (reader == null) return null;
41         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc e) {
48             throw new SQLException JavaDoc( e.getMessage() );
49         }
50         return result.toString();
51     }
52
53     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
54         if (value != null) {
55             String JavaDoc string = (String JavaDoc) value;
56             StringReader JavaDoc reader = new StringReader JavaDoc( string );
57             st.setCharacterStream( index, reader, string.length() );
58         }
59         else {
60             st.setNull( index, sqlTypes()[0] );
61         }
62     }
63
64     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
65         //returning value should be OK since String are immutable
66
return value;
67     }
68
69     public boolean isMutable() {
70         return false;
71     }
72
73     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
74         return (Serializable JavaDoc) value;
75     }
76
77     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException {
78         return cached;
79     }
80
81     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) throws HibernateException {
82         return original;
83     }
84 }
85
Popular Tags