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
31
32
33
34
35
36 public class PersistentLocalDate implements EnhancedUserType, Serializable {
37
38 public static final PersistentLocalDate INSTANCE = new PersistentLocalDate();
39
40 private static final int[] SQL_TYPES = new int[] { Types.DATE, };
41
42 public int[] sqlTypes() {
43 return SQL_TYPES;
44 }
45
46 public Class returnedClass() {
47 return LocalDate.class;
48 }
49
50 public boolean equals(Object x, Object y) throws HibernateException {
51 if (x == y) {
52 return true;
53 }
54 if (x == null || y == null) {
55 return false;
56 }
57 LocalDate dtx = (LocalDate) x;
58 LocalDate dty = (LocalDate) y;
59 return dtx.equals(dty);
60 }
61
62 public int hashCode(Object object) throws HibernateException {
63 return object.hashCode();
64 }
65
66 public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
67 return nullSafeGet(resultSet, strings[0]);
68
69 }
70
71 public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException {
72 Object timestamp = StandardBasicTypes.DATE.nullSafeGet(resultSet, string);
73 if (timestamp == null) {
74 return null;
75 }
76 if (timestamp instanceof Date) {
77 return LocalDate.fromDateFields((Date) timestamp);
78 } else if (timestamp instanceof Calendar) {
79 return LocalDate.fromCalendarFields((Calendar) timestamp);
80 }
81 return new LocalDate(timestamp);
82 }
83
84 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
85 if (value == null) {
86 StandardBasicTypes.DATE.nullSafeSet(preparedStatement, null, index);
87 } else {
88 StandardBasicTypes.DATE.nullSafeSet(preparedStatement, ((LocalDate) 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 LocalDate(string);
122 }
123
124 }