2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
25
package org.hibernate.type;
27
import java.io.Serializable;
28
import java.sql.PreparedStatement;
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
32
import org.hibernate.EntityMode;
33
import org.hibernate.Hibernate;
34
import org.hibernate.HibernateException;
35
import org.hibernate.engine.SessionImplementor;
36
import org.hibernate.util.SerializationHelper;
39
* <tt>serializable</tt>: A type that maps an SQL VARBINARY to a
40
* serializable Java object.
43
public class SerializableType extends MutableType {
45
private final Class serializableClass;
47
public SerializableType(Class serializableClass) {
48
this.serializableClass = serializableClass;
51
public void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
52
Hibernate.BINARY.set(st, toBytes(value), index);
55
public Object get(ResultSet rs, String name) throws HibernateException, SQLException {
56
byte[] bytes = (byte[]) Hibernate.BINARY.get(rs, name);
57
// Some JDBC drivers erroneously return an empty array here for a null DB value :/
58
if ( bytes == null || bytes.length == 0 ) {
62
return fromBytes(bytes);
66
public Class getReturnedClass() {
67
return serializableClass;
70
public boolean isEqual(Object x, Object y) throws HibernateException {
74
if ( x == null || y == null ) {
77
return x.equals( y ) || Hibernate.BINARY.isEqual( toBytes( x ), toBytes( y ) );
80
public int getHashCode(Object x, EntityMode entityMode) {
81
return Hibernate.BINARY.getHashCode( toBytes(x), entityMode );
84
public String toString(Object value) throws HibernateException {
85
return Hibernate.BINARY.toString( toBytes(value) );
88
public Object fromStringValue(String xml) throws HibernateException {
89
return fromBytes( (byte[]) Hibernate.BINARY.fromStringValue(xml) );
92
public String getName() {
93
return (serializableClass==Serializable.class) ? "serializable" : serializableClass.getName();
96
public Object deepCopyNotNull(Object value) throws HibernateException {
97
return fromBytes( toBytes(value) );
100
private static byte[] toBytes(Object object) throws SerializationException {
101
return SerializationHelper.serialize( (Serializable) object );
104
private static Object fromBytes( byte[] bytes ) throws SerializationException {
105
return SerializationHelper.deserialize(bytes);
108
public int sqlType() {
109
return Hibernate.BINARY.sqlType();
112
public Object assemble(Serializable cached, SessionImplementor session, Object owner)
113
throws HibernateException {
114
return (cached==null) ? null : fromBytes( (byte[]) cached );
117
public Serializable disassemble(Object value, SessionImplementor session, Object owner)
118
throws HibernateException {
119
return (value==null) ? null : toBytes(value);