1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.contrib.hibernate;
17
18 import java.io.Serializable;
19 import java.sql.PreparedStatement;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.sql.Types;
23
24 import org.hibernate.HibernateException;
25 import org.hibernate.type.StandardBasicTypes;
26 import org.hibernate.usertype.UserType;
27
28
29
30
31 public abstract class AbstractStringBasedJodaType implements UserType, Serializable {
32
33 private static final int[] SQL_TYPES = new int[] { Types.VARCHAR };
34
35 public int[] sqlTypes() {
36 return SQL_TYPES;
37 }
38
39 public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
40 String s = (String) StandardBasicTypes.STRING.nullSafeGet(resultSet, strings[0]);
41 if (s == null) {
42 return null;
43 }
44
45 return fromNonNullString(s);
46 }
47
48 protected abstract Object fromNonNullString(String s);
49
50 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
51 if (value == null) {
52 StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index);
53 } else {
54 StandardBasicTypes.STRING.nullSafeSet(preparedStatement, toNonNullString(value), index);
55 }
56 }
57
58 protected abstract String toNonNullString(Object value);
59
60 public boolean equals(Object x, Object y) throws HibernateException {
61 if (x == y) {
62 return true;
63 }
64 if (x == null || y == null) {
65 return false;
66 }
67 return x.equals(y);
68 }
69
70 public int hashCode(Object object) throws HibernateException {
71 return object.hashCode();
72 }
73
74 public Object deepCopy(Object value) throws HibernateException {
75 return value;
76 }
77
78 public boolean isMutable() {
79 return false;
80 }
81
82 public Serializable disassemble(Object value) throws HibernateException {
83 return (Serializable) value;
84 }
85
86 public Object assemble(Serializable cached, Object value) throws HibernateException {
87 return cached;
88 }
89
90 public Object replace(Object original, Object target, Object owner) throws HibernateException {
91 return original;
92 }
93
94 }