001 /*
002 * Copyright 2001-2009 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.Types;
023
024 import org.hibernate.Hibernate;
025 import org.hibernate.HibernateException;
026 import org.hibernate.usertype.EnhancedUserType;
027 import org.joda.time.LocalDateTime;
028
029 /**
030 * Persist {@link org.joda.time.LocalDateTime} via hibernate.
031 *
032 * @author Mario Ivankovits (mario@ops.co.at)
033 */
034 public class PersistentLocalDateTime implements EnhancedUserType, Serializable {
035
036 public static final PersistentLocalDateTime INSTANCE = new PersistentLocalDateTime();
037
038 private static final int[] SQL_TYPES = new int[] { Types.TIMESTAMP, };
039
040 public int[] sqlTypes() {
041 return SQL_TYPES;
042 }
043
044 public Class returnedClass() {
045 return LocalDateTime.class;
046 }
047
048 public boolean equals(Object x, Object y) throws HibernateException {
049 if (x == y) {
050 return true;
051 }
052 if (x == null || y == null) {
053 return false;
054 }
055 LocalDateTime dtx = (LocalDateTime) x;
056 LocalDateTime dty = (LocalDateTime) y;
057 return dtx.equals(dty);
058 }
059
060 public int hashCode(Object object) throws HibernateException {
061 return object.hashCode();
062 }
063
064 public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
065 return nullSafeGet(resultSet, strings[0]);
066 }
067
068 public Object nullSafeGet(ResultSet resultSet, String string) throws SQLException {
069 Object timestamp = Hibernate.TIMESTAMP.nullSafeGet(resultSet, string);
070 if (timestamp == null) {
071 return null;
072 }
073 return new LocalDateTime(timestamp);
074 }
075
076 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
077 if (value == null) {
078 Hibernate.TIMESTAMP.nullSafeSet(preparedStatement, null, index);
079 } else {
080 Hibernate.TIMESTAMP.nullSafeSet(preparedStatement, ((LocalDateTime) value).toDateTime().toDate(), index);
081 }
082 }
083
084 public Object deepCopy(Object value) throws HibernateException {
085 return value;
086 }
087
088 public boolean isMutable() {
089 return false;
090 }
091
092 public Serializable disassemble(Object value) throws HibernateException {
093 return (Serializable) value;
094 }
095
096 public Object assemble(Serializable cached, Object value) throws HibernateException {
097 return cached;
098 }
099
100 public Object replace(Object original, Object target, Object owner) throws HibernateException {
101 return original;
102 }
103
104 public String objectToSQLString(Object object) {
105 throw new UnsupportedOperationException();
106 }
107
108 public String toXMLString(Object object) {
109 return object.toString();
110 }
111
112 public Object fromXMLString(String string) {
113 return new LocalDateTime(string);
114 }
115
116 }