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.math.BigInteger;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.sql.Types;
24
25 import org.hibernate.HibernateException;
26 import org.hibernate.type.StandardBasicTypes;
27 import org.hibernate.usertype.UserType;
28 import org.joda.time.Duration;
29
30
31
32
33
34
35
36 public class PersistentDurationAsMilliseconds implements UserType, Serializable {
37
38 private static final int[] SQL_TYPES = new int[] { Types.BIGINT };
39
40 public Class returnedClass() {
41 return Duration.class;
42 }
43
44 public int[] sqlTypes() {
45 return SQL_TYPES;
46 }
47
48 public Object nullSafeGet(ResultSet resultSet, String[] strings,
49 Object object) throws HibernateException, SQLException {
50 BigInteger b = (BigInteger) StandardBasicTypes.BIG_INTEGER.nullSafeGet(
51 resultSet, strings[0]);
52 if (b == null) {
53 return null;
54 }
55
56 return new Duration(b.longValue());
57 }
58
59 public void nullSafeSet(PreparedStatement preparedStatement, Object value,
60 int index) throws HibernateException, SQLException {
61 if (value == null) {
62 StandardBasicTypes.BIG_INTEGER.nullSafeSet(preparedStatement, null,
63 index);
64 } else {
65 StandardBasicTypes.BIG_INTEGER.nullSafeSet(preparedStatement,
66 BigInteger.valueOf((Long) value), index);
67 }
68 }
69
70 public boolean equals(Object x, Object y) throws HibernateException {
71 if (x == y) {
72 return true;
73 }
74 if (x == null || y == null) {
75 return false;
76 }
77 return x.equals(y);
78 }
79
80 public int hashCode(Object object) throws HibernateException {
81 return object.hashCode();
82 }
83
84 public Object deepCopy(Object value) throws HibernateException {
85 return value;
86 }
87
88 public boolean isMutable() {
89 return false;
90 }
91
92 public Serializable disassemble(Object value) throws HibernateException {
93 return (Serializable) value;
94 }
95
96 public Object assemble(Serializable cached, Object value)
97 throws HibernateException {
98 return cached;
99 }
100
101 public Object replace(Object original, Object target, Object owner)
102 throws HibernateException {
103 return original;
104 }
105
106 }