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