rowmapper query example establish ejemplos cannot java spring postgresql jdbc intervals

java - query - jdbctemplate spring ejemplos



¿Cómo operar en el tipo de datos de intervalo PostgreSQL usando jdbc/spring-jdbc no usando PGInterval? (1)

Los tipos de datos de fecha y hora de Java son como todos saben, no hay necesidad de centrarse en eso.

Sin embargo, cuando estoy usando JDBC o extensiones basadas en Spring, como SimpleJdbcTemplate para recuperar y almacenar valores de intervalo , ¿qué tipo de Java debo usar, si no quiero usar la clase org.postgresql.util.PGInterval?

Esta clase es interna del controlador PostgreSQL, por lo que su uso haría que el código fuera específico del DB. Creo que debería ser posible operar en intervalos de forma independiente de DB, porque es uno de los tipos de SQL estándar.


Un intervalo no es uno de los tipos de JDBC estándar, como se indica en la clase java.sql.Types . Sé que si llamas resultSet.getObject("interval_column") , es un PGInterval cuando se PGInterval , por lo que parece que el controlador PG JDBC podría estar forzando tu mano para tratar con eso como tal, a menos que hagas lo que Glenn dice y convierte a una cadena, o tal vez un número, en su SQL.

En nuestra aplicación, usamos JodaTime para toda nuestra gestión de fechas, y tenemos un tipo de hibernación escrito que convierte nuestra propiedad de bean desde y hacia un PGInterval , y usamos getObject y setObject para comunicarnos con JDBC. Dudo que el código lo ayude a lidiar con lo que está buscando aquí, pero puedo compartirlo con usted si está interesado.

ACTUALIZADO: Aquí está la clase de tipo de hibernación que se convierte entre Joda Time y PGInterval. Sé que esto no responde a la pregunta, pero el cartel original solicitó el código de muestra.

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"); } }