001 /* 002 * Copyright 2001-2011 Stephen Colebourne 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.joda.time.contrib.hibernate; 017 018 import java.io.Serializable; 019 import java.sql.PreparedStatement; 020 import java.sql.ResultSet; 021 import java.sql.SQLException; 022 import java.sql.Time; 023 import java.sql.Types; 024 025 import org.hibernate.HibernateException; 026 import org.hibernate.type.StandardBasicTypes; 027 import org.hibernate.usertype.EnhancedUserType; 028 import org.joda.time.DateTime; 029 import org.joda.time.TimeOfDay; 030 031 /** 032 * Persist {@link org.joda.time.TimeOfDay} via hibernate. 033 * This uses java.sql.Time and the time datatype of your database. 034 * Notice: You might loose the milliseconds part. 035 * 036 * @author Mario Ivankovits (mario@ops.co.at) 037 */ 038 public class PersistentTimeOfDay implements EnhancedUserType, Serializable { 039 040 private final DateTime timeBase = new DateTime(1970, 1, 1, 0, 0, 0, 0); 041 042 public static final PersistentTimeOfDay INSTANCE = new PersistentTimeOfDay(); 043 044 private static final int[] SQL_TYPES = new int[] { Types.TIME, }; 045 046 public int[] sqlTypes() { 047 return SQL_TYPES; 048 } 049 050 public Class returnedClass() { 051 return TimeOfDay.class; 052 } 053 054 public boolean equals(Object x, Object y) throws HibernateException { 055 if (x == y) { 056 return true; 057 } 058 if (x == null || y == null) { 059 return false; 060 } 061 TimeOfDay dtx = (TimeOfDay) x; 062 TimeOfDay dty = (TimeOfDay) y; 063 return dtx.equals(dty); 064 } 065 066 public int hashCode(Object object) throws HibernateException { 067 return object.hashCode(); 068 } 069 070 public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException { 071 return nullSafeGet(resultSet, strings[0]); 072 073 } 074 075 public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException { 076 Object date = StandardBasicTypes.TIME.nullSafeGet(resultSet, string); 077 if (date == null) { 078 return null; 079 } 080 081 return new TimeOfDay(date); 082 } 083 084 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException { 085 if (value == null) { 086 StandardBasicTypes.TIME.nullSafeSet(preparedStatement, null, index); 087 } else { 088 StandardBasicTypes.TIME.nullSafeSet(preparedStatement, 089 new Time(((TimeOfDay) value).toDateTime(timeBase).getMillis()), index); 090 } 091 } 092 093 public Object deepCopy(Object value) throws HibernateException { 094 return value; 095 } 096 097 public boolean isMutable() { 098 return false; 099 } 100 101 public Serializable disassemble(Object value) throws HibernateException { 102 return (Serializable) value; 103 } 104 105 public Object assemble(Serializable cached, Object value) throws HibernateException { 106 return cached; 107 } 108 109 public Object replace(Object original, Object target, Object owner) throws HibernateException { 110 return original; 111 } 112 113 public String objectToSQLString(Object object) { 114 throw new UnsupportedOperationException(); 115 } 116 117 public String toXMLString(Object object) { 118 return object.toString(); 119 } 120 121 public Object fromXMLString(String string) { 122 return new TimeOfDay(string); 123 } 124 125 }