Dan*_*lor 19 java postgresql spring jdbc intervals
每个人都知道Java日期和时间数据类型,没有必要关注它.
但是,当我使用JDBC或基于Spring的扩展(如SimpleJdbcTemplate)来检索和存储间隔值时,我应该使用哪种Java类型,如果我不想使用org.postgresql.util.PGInterval类?
这个类是PostgreSQL驱动程序的内部,因此使用它会使代码特定于DB.我认为应该可以以DB无关的方式间隔操作,因为它是标准SQL类型之一.
j.s*_*der 14
的间隔不是标准的JDBC类型之一,如在所列出的java.sql.Types类.我知道,如果你打电话resultSet.getObject("interval_column"),这是一个PGInterval铸造,所以看起来PG JDBC驱动程序可能会强迫你的手处理它,除非你做Glenn所说的并将其转换为字符串,或者可能是一个数字,在你的SQL中.
在我们的应用中,我们使用JodaTime为我们所有的数据管理的,我们有一个Hibernate类型撰文指出,我们的bean属性转换和从PGInterval,并使用getObject和setObject使用JDBC进行沟通.我怀疑代码可以帮助你处理你在这里寻找的东西,但如果你有兴趣我可以与你分享.
更新:这是在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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12340 次 |
| 最近记录: |