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
97
98
99
100
101
102
/*
* @(#)MutableUserType.java 25 Feb 2009
*
* 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 org.apache.commons.lang.ObjectUtils;
import org.hibernate.HibernateException;
import org.hibernate.type.SerializationException;
import org.hibernate.usertype.UserType;

/**
* A skeleton Hibernate {@link UserType}. Assumes, by default, that the return
* type is mutable. Subtypes whose {@code deepCopy} implementation returns a
* non-serializable object <strong>must override</strong> {@link #disassemble(Object)}.
* <p>
* User types returning only <em>im</em>mutable objects should extend
* {@link ImmutableUserType}.
*
* @author anph
* @since 25 Feb 2009
*
*/
public abstract class MutableUserType implements UserType {

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return true;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
return ObjectUtils.equals(x, y);
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object x) throws HibernateException {
assert (x != null);
return x.hashCode();
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
// also safe for mutable objects
return deepCopy(cached);
}

/**
* Disassembles the object in preparation for serialization.
* See {@link org.hibernate.usertype.UserType#disassemble(java.lang.Object)}.
* <p>
* Expects {@link #deepCopy(Object)} to return a {@code Serializable}.
* <strong>Subtypes whose {@code deepCopy} implementation returns a
* non-serializable object must override this method.</strong>
*/
public Serializable disassemble(Object value) throws HibernateException {
// also safe for mutable objects
Object deepCopy = deepCopy(value);

if (!(deepCopy instanceof Serializable)) {
throw new SerializationException(
String.format("deepCopy of %s is not serializable", value), null);
}

return (Serializable) deepCopy;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
// also safe for mutable objects
return deepCopy(original);
}

}

Change log

r615 by sharedocs1 on Nov 9, 2009   Diff
Now throwing an exception in the default
"disassemble" implementation if deepCopy
doesn't return a Serializable, and
improved documentation.
Go to: 
Project members, sign in to write a code review

Older revisions

r515 by sharedocs1 on Nov 3, 2009   Diff
Made implementations that shouldn't be
overridden final and added
documentation for a condition of the
deepCopy implementation.
r480 by sharedocs1 on Nov 2, 2009   Diff
Factored the dirty checking option for
mutable types out into a separate
UserType.
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: 3627 bytes, 102 lines
Powered by Google Project Hosting