An khoảng thời gian không phải là một trong các loại JDBC chuẩn, như được liệt kê trong lớp java.sql.Types
. Tôi biết rằng nếu bạn gọi resultSet.getObject("interval_column")
, nó là PGInterval
khi được đúc, vì vậy có vẻ như trình điều khiển PG JDBC có thể buộc bàn tay của bạn xử lý nó như vậy, trừ khi bạn làm những gì Glenn nói và chuyển đổi nó thành chuỗi hoặc một số, trong SQL của bạn.
Trong ứng dụng của chúng tôi, chúng tôi sử dụng JodaTime cho tất cả các quản lý ngày của chúng tôi, và chúng tôi có một loại Hibernate bằng văn bản có thể chuyển đổi tài sản đậu của chúng tôi đến và đi từ một PGInterval
, và sử dụng getObject
và setObject
để giao tiếp với JDBC. Tôi nghi ngờ rằng mã sẽ giúp bạn đối phó với những gì bạn đang tìm kiếm ở đây, nhưng tôi có thể chia sẻ nó với bạn nếu bạn quan tâm.
CẬP NHẬT: Đây là lớp Kiểu Hibernate chuyển đổi giữa Thời gian Joda và PGInterval. Tôi biết điều này không trả lời câu hỏi, nhưng poster ban đầu đã yêu cầu mã mẫu.
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");
}
}
Để tránh phụ thuộc vào tiện ích mở rộng, tôi thường sửa đổi truy vấn sql để trả về cột Chuỗi thay vì loại tùy chỉnh (nếu có thể). Vì vậy, trong trường hợp này, một "to_char (interval, formatString)", sau đó sử dụng trình ánh xạ hàng ở phía máy khách để phân tích cú pháp và tạo lại String đã nhận thành một đối tượng thích hợp. Không phải thanh lịch nhất, nhưng thường đơn giản. – Glenn