My favorites | Sign in
Project Home Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* @(#)XStreamableUserType.java Oct 3, 2007
*
* Copyright © 2009 Andrew Phillips.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.qrmedia.commons.persistence.hibernate.usertype;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

import com.thoughtworks.xstream.XStream;

/**
* A {@link UserType} that persists objects as {@link XStream XStream} XML.
* <p>
* Unlike the default JPA object mapping, {@code XStreamableUserType} can also be used
* for properties that do not implement {@link Serializable}. For restrictions on the types
* of objects that can be handled by this user type see the <a href="http://xstream.codehaus.org">XStream documentation</a>.
* <p>
* Users intending to use this type for mutable non-<code>Collection</code> objects
* should override {@link #deepCopyValue(Object)} to correctly return a <u>copy</u>
* of the object.
*
* @author rvd
* @author anph
* @since Oct 3, 2007
*
*/
public class XStreamableUserType extends CollectionReturningUserType {
// thread-safe, according to the XStream documentation
private static final XStream XSTREAM = new XStream();

/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class<Object> returnedClass() {
return Object.class;
}

/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
return XSTREAM.fromXML((String) Hibernate.STRING.nullSafeGet(resultSet, names[0]));
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException {
Hibernate.STRING.nullSafeSet(preparedStatement, XSTREAM.toXML(value), index);
}

/* (non-Javadoc)
* @see com.qrmedia.commons.persistence.hibernate.usertype.AbstractMutableCollectionReturningUserType#deepCopyValue(java.lang.Object)
*/
@Override
protected Object deepCopyValue(Object value) {
// only collections are properly deep copied at present, and the superclass handles that
return value;
}

}

Change log

r531 by sharedocs1 on Nov 7, 2009   Diff
Avoided dirty-checking null objects and
renamed the TypesafeObjectUserType to
XStreamableType (in analogy to Hibernate's
SerializableType).
Go to: 
Project members, sign in to write a code review

Older revisions

r480 by sharedocs1 on Nov 2, 2009   Diff
Factored the dirty checking option for
mutable types out into a separate
UserType.
r83 by sharedocs1 on Oct 9, 2009   Diff
Updated Javadoc.
r82 by sharedocs1 on Oct 6, 2009   Diff
Tightened up the conditions on one of
the tests and renamed all the
Abstract* classes, removing the
superfluous Abstract from the name.
See one of Adam Bien's blog posts.
All revisions of this file

File info

Size: 3432 bytes, 96 lines
Powered by Google Project Hosting