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