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.EnhancedUserType;
27 import org.joda.time.LocalDateTime;
28
29
30
31
32
33
34 public class PersistentLocalDateTime implements EnhancedUserType, Serializable {
35
36 public static final PersistentLocalDateTime INSTANCE = new PersistentLocalDateTime();
37
38 private static final int[] SQL_TYPES = new int[] { Types.TIMESTAMP, };
39
40 public int[] sqlTypes() {
41 return SQL_TYPES;
42 }
43
44 public Class returnedClass() {
45 return LocalDateTime.class;
46 }
47
48 public boolean equals(Object x, Object y) throws HibernateException {
49 if (x == y) {
50 return true;
51 }
52 if (x == null || y == null) {
53 return false;
54 }
55 LocalDateTime dtx = (LocalDateTime) x;
56 LocalDateTime dty = (LocalDateTime) y;
57 return dtx.equals(dty);
58 }
59
60 public int hashCode(Object object) throws HibernateException {
61 return object.hashCode();
62 }
63
64 public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
65 return nullSafeGet(resultSet, strings[0]);
66 }
67
68 public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException {
69 Object timestamp = StandardBasicTypes.TIMESTAMP.nullSafeGet(resultSet, string);
70 if (timestamp == null) {
71 return null;
72 }
73 return new LocalDateTime(timestamp);
74 }
75
76 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
77 if (value == null) {
78 StandardBasicTypes.TIMESTAMP.nullSafeSet(preparedStatement, null, index);
79 } else {
80 StandardBasicTypes.TIMESTAMP.nullSafeSet(preparedStatement, ((LocalDateTime) value).toDateTime().toDate(), index);
81 }
82 }
83
84 public Object deepCopy(Object value) throws HibernateException {
85 return value;
86 }
87
88 public boolean isMutable() {
89 return false;
90 }
91
92 public Serializable disassemble(Object value) throws HibernateException {
93 return (Serializable) value;
94 }
95
96 public Object assemble(Serializable cached, Object value) throws HibernateException {
97 return cached;
98 }
99
100 public Object replace(Object original, Object target, Object owner) throws HibernateException {
101 return original;
102 }
103
104 public String objectToSQLString(Object object) {
105 throw new UnsupportedOperationException();
106 }
107
108 public String toXMLString(Object object) {
109 return object.toString();
110 }
111
112 public Object fromXMLString(String string) {
113 return new LocalDateTime(string);
114 }
115
116 }