KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ClobType.java,v 1.14 2005/07/19 18:17:14 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.Clob JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.sql.Types JavaDoc;
10 import java.util.Map JavaDoc;
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 /**
25  * <tt>clob</tt>: A type that maps an SQL CLOB to a java.sql.Clob.
26  * @author Gavin King
27  */

28 public class ClobType extends AbstractType {
29
30     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index, SessionImplementor session)
31     throws HibernateException, SQLException JavaDoc {
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 JavaDoc) value);
51             }
52             
53         }
54         
55     }
56
57     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
58         Clob JavaDoc value = rs.getClob(name);
59         return rs.wasNull() ? null : new SerializableClob(value);
60     }
61
62     public Class JavaDoc getReturnedClass() {
63         return Clob JavaDoc.class;
64     }
65
66     public boolean isEqual(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode) {
67         return x == y;
68     }
69     
70     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
71         return System.identityHashCode(x);
72     }
73
74     public int compare(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode) {
75         return 0; //lobs cannot be compared
76
}
77
78     public String JavaDoc getName() {
79         return "clob";
80     }
81     
82     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
83         throws HibernateException {
84         throw new UnsupportedOperationException JavaDoc("Clobs are not cacheable");
85     }
86
87     public Object JavaDoc deepCopy(Object JavaDoc value, EntityMode entityMode, SessionFactoryImplementor factory) {
88         return value;
89     }
90     
91     public Object JavaDoc 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 JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc name,
104             SessionImplementor session, Object JavaDoc owner)
105             throws HibernateException, SQLException JavaDoc {
106         return get(rs, name);
107     }
108     
109     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names,
110             SessionImplementor session, Object JavaDoc owner)
111             throws HibernateException, SQLException JavaDoc {
112         return get( rs, names[0] );
113     }
114     
115     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index,
116             boolean[] settable, SessionImplementor session)
117             throws HibernateException, SQLException JavaDoc {
118         if ( settable[0] ) set(st, value, index, session);
119     }
120     
121     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index,
122             SessionImplementor session) throws HibernateException, SQLException JavaDoc {
123         set(st, value, index, session);
124     }
125     
126     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target,
127             SessionImplementor session, Object JavaDoc owner, Map JavaDoc copyCache)
128             throws HibernateException {
129         //Clobs are ignored by merge() operation
130
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 JavaDoc value, SessionFactoryImplementor factory) {
138         if (value!=null) {
139             Clob JavaDoc clob = (Clob JavaDoc) value;
140             try {
141                 int len = (int) clob.length();
142                 node.setText( clob.getSubString(0, len) );
143             }
144             catch (SQLException JavaDoc sqle) {
145                 throw new HibernateException("could not read XML from Clob", sqle);
146             }
147         }
148     }
149     
150     public String JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory)
151             throws HibernateException {
152         return value==null ? "null" : value.toString();
153     }
154
155     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
156         return value==null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
157     }
158
159 }
160
161
162
163
164
165
166
Popular Tags