繁体   English   中英

如何使用jdbc / spring-jdbc不使用PGInterval对PostgreSQL间隔数据类型进行操作?

[英]How to operate on PostgreSQL interval datatype using jdbc/spring-jdbc not using PGInterval?

每个人都知道Java日期和时间数据类型,没有必要关注它。

但是,当我使用JDBC或基于Spring的扩展(如SimpleJdbcTemplate)来检索和存储间隔值时,我应该使用哪种Java类型,如果我不想使用org.postgresql.util.PGInterval类?

这个类是PostgreSQL驱动程序的内部,因此使用它会使代码特定于DB。 我认为应该可以以DB无关的方式间隔操作,因为它是标准SQL类型之一。

间隔不是标准JDBC类型之一,如java.sql.Types类中所列。 我知道如果你调用resultSet.getObject("interval_column") ,它在PGInterval时是一个PGInterval ,所以它看起来像PG JDBC驱动程序可能会强迫你的手处理它,除非你做Glenn所说的并转换它在SQL中的字符串或数字。

在我们的应用程序中,我们使用JodaTime进行所有日期管理,我们编写了一个Hibernate Type,它将我们的bean属性转换为PGInterval ,并使用getObjectsetObject与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");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM