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