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