KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SerializableType.java,v 1.7 2005/03/16 04:45:25 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import org.hibernate.EntityMode;
10 import org.hibernate.Hibernate;
11 import org.hibernate.HibernateException;
12 import org.hibernate.engine.SessionImplementor;
13 import org.hibernate.util.SerializationHelper;
14
15 /**
16  * <tt>serializable</tt>: A type that maps an SQL VARBINARY to a
17  * serializable Java object.
18  * @author Gavin King
19  */

20 public class SerializableType extends MutableType {
21
22     private final Class JavaDoc serializableClass;
23
24     public SerializableType(Class JavaDoc serializableClass) {
25         this.serializableClass = serializableClass;
26     }
27
28     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
29         Hibernate.BINARY.set(st, toBytes(value), index);
30     }
31
32     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
33
34         byte[] bytes = (byte[]) Hibernate.BINARY.get(rs, name);
35         if ( bytes==null ) {
36             return null;
37         }
38         else {
39             return fromBytes(bytes);
40         }
41     }
42
43     public Class JavaDoc getReturnedClass() {
44         return serializableClass;
45     }
46
47     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
48         if (x==y) return true;
49         if (x==null || y==null) return false;
50         return Hibernate.BINARY.isEqual( toBytes(x), toBytes(y) );
51     }
52
53     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
54         return Hibernate.BINARY.getHashCode( toBytes(x), entityMode );
55     }
56
57     public String JavaDoc toString(Object JavaDoc value) throws HibernateException {
58         return Hibernate.BINARY.toString( toBytes(value) );
59     }
60
61     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
62         return fromBytes( (byte[]) Hibernate.BINARY.fromStringValue(xml) );
63     }
64
65     public String JavaDoc getName() {
66         return (serializableClass==Serializable JavaDoc.class) ? "serializable" : serializableClass.getName();
67     }
68
69     public Object JavaDoc deepCopyNotNull(Object JavaDoc value) throws HibernateException {
70         return fromBytes( toBytes(value) );
71     }
72
73     private static byte[] toBytes(Object JavaDoc object) throws SerializationException {
74         return SerializationHelper.serialize( (Serializable JavaDoc) object );
75     }
76
77     private static Object JavaDoc fromBytes( byte[] bytes ) throws SerializationException {
78         return SerializationHelper.deserialize(bytes);
79     }
80
81     public int sqlType() {
82         return Hibernate.BINARY.sqlType();
83     }
84
85     public Object JavaDoc assemble(Serializable JavaDoc cached, SessionImplementor session, Object JavaDoc owner)
86     throws HibernateException {
87         return (cached==null) ? null : fromBytes( (byte[]) cached );
88     }
89
90     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
91     throws HibernateException {
92         return (value==null) ? null : toBytes(value);
93     }
94
95 }
96
97
98
99
100
101
102
103
Popular Tags