View Javadoc
1   /*
2    *  Copyright 2001-2012 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * Converts a org.joda.time.Duration to and from Sql for Hibernate. It simply
32   * stores the milliseconds as a bigint.
33   * 
34   * @author Daniel Jurado (jurado@gmail.com)
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 }