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