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