Here are a couple classes I wrote that don't exist yet in the Hibernate
contrib project - please add and mod as you wish. I don't think I can
send mail with attached files to the list...
I have not thoroughly tested them yet, since I'm still in development
mode so ymmv.
Connor
------------------------------ PersistentDateTimeZone - persists a
DateTimeZone
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import org.joda.time.DateTimeZone;
/*
* Hibernate user type that persists a [EMAIL PROTECTED]
org.joda.time.DateTimeZone
DateTimeZone}.
* @author Connor Barry (my first name at slickapps.com)
*/
public class PersistentDateTimeZone implements UserType {
private static final int[] SQL_TYPES = { Types.VARCHAR };
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class<DateTimeZone> returnedClass() {
return DateTimeZone.class;
}
public Object nullSafeGet(ResultSet resultSet, String[] names,
Object owner)
throws HibernateException, SQLException {
String name = resultSet.getString(names[0]);
if (resultSet.wasNull()) return null;
try {
return DateTimeZone.forID(name);
} catch (IllegalArgumentException e) {
throw new HibernateException("The persistent time zone ID of
'" + name
+ "' is not a recognized time zone");
}
}
public void nullSafeSet(PreparedStatement preparedStatement, Object
value,
int index) throws HibernateException, SQLException {
if (value == null) preparedStatement.setNull(index, Types.VARCHAR);
else preparedStatement.setString(index, ((DateTimeZone)
value).getID());
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached;
}
public Serializable disassemble(Object value) throws
HibernateException {
return (Serializable) value;
}
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) return true;
if (x == null || y == null) return false;
return x.equals(y);
}
}
------------------------------ PersistentIntervalTZ - persists an
interval and time zone into separate columns
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.joda.time.contrib.hibernate.PersistentInterval;
/**
* Persist an [EMAIL PROTECTED] org.joda.time.Interval Interval} via
hibernate.<br />
* Columns 1 and 2 will contain the start and end millis. Column 3 will
contain
* the TimeZone ID.
*
* @author Connor Barry (my first name at slickapps.com)
*/
public class PersistentIntervalTZ extends PersistentInterval {
private static final String[] PROPERTY_NAMES = new String[] { "start",
"end", "timezone" };
private static final Type[] TYPES = new Type[] { Hibernate.TIMESTAMP,
Hibernate.TIMESTAMP, Hibernate.STRING };
@Override
public String[] getPropertyNames() {
return PROPERTY_NAMES;
}
@Override
public Type[] getPropertyTypes() {
return TYPES;
}
@Override
public Object getPropertyValue(Object component, int property)
throws HibernateException {
Interval interval = (Interval) component;
if (property == 0) return interval.getStart().toDate();
if (property == 1) return interval.getEnd().toDate();
if (property == 2) return interval.getChronology().getZone();
throw new HibernateException("Unexpected property index " +
property);
}
@Override
public Object nullSafeGet(ResultSet resultSet, String[] names,
SessionImplementor session, Object owner) throws
HibernateException,
SQLException {
if (resultSet == null) return null;
if (names.length < 3)
throw new HibernateException("The names array didn't contain
3 items");
Timestamp start = (Timestamp)
Hibernate.TIMESTAMP.nullSafeGet(resultSet,
names[0]);
Timestamp end = (Timestamp) Hibernate.STRING.nullSafeGet(resultSet,
names[1]);
String timeZone = (String) Hibernate.STRING.nullSafeGet(resultSet,
names[2]);
if (start == null || end == null) return null;
if (timeZone == null)
return new Interval(start.getTime(), end.getTime());
return new Interval(start.getTime(), end.getTime(), DateTimeZone
.forID(timeZone));
}
@Override
public void nullSafeSet(PreparedStatement statement, Object value,
int index, SessionImplementor session) throws
HibernateException,
SQLException {
if (value == null) {
statement.setNull(index, Hibernate.TIMESTAMP.sqlType());
statement.setNull(index + 1, Hibernate.TIMESTAMP.sqlType());
statement.setNull(index + 2, Hibernate.STRING.sqlType());
return;
}
Interval interval = (Interval) value;
statement.setTimestamp(index, new Timestamp(interval.getStart()
.getMillis()));
statement.setTimestamp(index + 1, new Timestamp(interval.getEnd()
.getMillis()));
statement
.setString(index + 2,
interval.getChronology().getZone().getID());
}
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest