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.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   * Persist {@link org.joda.time.DateTime} via hibernate as a BIGINT.
30   * 
31   * @author Martin Grove (marting@optrak.co.uk))
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     // __________ EnhancedUserType ____________________
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 }