简体   繁体   English

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

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

Java date & time datatypes are as everybody knows, there's no need to focus on that. 每个人都知道Java日期和时间数据类型,没有必要关注它。

However, when I'm using JDBC or Spring-based extensions, such as SimpleJdbcTemplate to retrieve and store interval values, what Java type should I use, if I don't want to use org.postgresql.util.PGInterval class? 但是,当我使用JDBC或基于Spring的扩展(如SimpleJdbcTemplate)来检索和存储间隔值时,我应该使用哪种Java类型,如果我不想使用org.postgresql.util.PGInterval类?

This class is internal of PostgreSQL driver so using it would make the code DB-specific. 这个类是PostgreSQL驱动程序的内部,因此使用它会使代码特定于DB。 I think it should be possible to operate on intervals in DB-agnostic way, because it is one of standard SQL types. 我认为应该可以以DB无关的方式间隔操作,因为它是标准SQL类型之一。

An interval isn't one of the standard JDBC Types, as listed in the java.sql.Types class. 间隔不是标准JDBC类型之一,如java.sql.Types类中所列。 I know that if you call resultSet.getObject("interval_column") , it is a PGInterval when casted, so it looks like the PG JDBC driver might be forcing your hand to deal with it as such, unless you do what Glenn says and convert it to a string, or maybe a number, in your SQL. 我知道如果你调用resultSet.getObject("interval_column") ,它在PGInterval时是一个PGInterval ,所以它看起来像PG JDBC驱动程序可能会强迫你的手处理它,除非你做Glenn所说的并转换它在SQL中的字符串或数字。

In our application, we use JodaTime for all of our date management, and we have a Hibernate Type written that converts our bean property to and from a PGInterval , and use getObject and setObject to communicate with JDBC. 在我们的应用程序中,我们使用JodaTime进行所有日期管理,我们编写了一个Hibernate Type,它将我们的bean属性转换为PGInterval ,并使用getObjectsetObject与JDBC进行通信。 I doubt that code would help you deal with what you are looking for here, but I can share it with you if you are interested. 我怀疑代码可以帮助你处理你在这里寻找的东西,但如果你有兴趣我可以与你分享。

UPDATED: Here is the Hibernate Type class that converts between Joda Time and PGInterval. 更新:这是在Joda Time和PGInterval之间转换的Hibernate Type类。 I know this doesn't answer the question, but the original poster asked for the sample code. 我知道这不回答这个问题,但原始海报要求提供示例代码。

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