KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: BlobType.java,v 1.14 2005/07/19 18:17:13 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.Blob 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.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 /**
24  * <tt>blob</tt>: A type that maps an SQL BLOB to a java.sql.Blob.
25  * @author Gavin King
26  */

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