间隔不是类中列出的标准 JDBC 类型之一。我知道,如果你调用 ,它是一个当转换时,所以看起来PG JDBC驱动程序可能会强迫你的手这样处理它,除非你按照Glenn所说的去做,并在你的SQL中将其转换为字符串,或者可能是一个数字。java.sql.Types
resultSet.getObject("interval_column")
PGInterval
在我们的应用程序中,我们使用 JodaTime 进行所有日期管理,并且我们编写了一个 Hibernate 类型,该类型将我们的 bean 属性转换为 和 与 , 以及 使用 JDBC 并与 JDBC 通信。我怀疑代码是否能帮助你处理你在这里寻找的东西,但如果你有兴趣,我可以与你分享。PGInterval
getObject
setObject
更新:这是在Joda Time和PGInterval之间转换的Hibernate Type类。我知道这并不能回答这个问题,但原始海报要求提供示例代码。
package com.your.package.hibernate.types;
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.DurationFieldType;
import org.joda.time.Period;
import org.joda.time.ReadableDuration;
import org.joda.time.ReadablePeriod;
import org.postgresql.util.PGInterval;
public class JodaTimeDurationType
implements UserType {
public Class<?> returnedClass() {
return ReadableDuration.class;
}
public int[] sqlTypes() {
return new int[] {Types.OTHER};
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
try {
final PGInterval pgi = (PGInterval)resultSet.getObject(names[0]);
final int years = pgi.getYears();
final int months = pgi.getMonths();
final int days = pgi.getDays();
final int hours = pgi.getHours();
final int mins = pgi.getMinutes();
final double secs = pgi.getSeconds();
return new Period(years, months, 0, days, hours, mins, (int)secs, 0).toStandardDuration();
}
catch (Exception e) {
return null;
}
}
public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
statement.setNull(index, Types.OTHER);
}
else {
final ReadablePeriod period = ((ReadableDuration)value).toPeriod();
final int years = period.get(DurationFieldType.years());
final int months = period.get(DurationFieldType.months());
final int days = period.get(DurationFieldType.days());
final int hours = period.get(DurationFieldType.hours());
final int mins = period.get(DurationFieldType.minutes());
final int secs = period.get(DurationFieldType.seconds());
final PGInterval pgi = new PGInterval(years, months, days, hours, mins, secs);
statement.setObject(index, pgi);
}
}
public boolean equals(Object x, Object y)
throws HibernateException {
return x == y;
}
public int hashCode(Object x)
throws HibernateException {
return x.hashCode();
}
public Object deepCopy(Object value)
throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value)
throws HibernateException {
throw new HibernateException("not implemented");
}
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
throw new HibernateException("not implemented");
}
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
throw new HibernateException("not implemented");
}
}