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