package framework.postgresql;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import org.postgresql.util.PGInterval;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.Duration;
/**
* PostgreSql Inteval字段与java.time.Duration映射
* 目前只支持到最多1个月(30天)的间隔
* <p>
* 使用方法:
* 在实体类上增加
* \@TypeDef(name="interval", typeClass = IntervalType.class)
* 在字段定义上增加:
* \@Type(type = "interval")
* <p>
* http://stackoverflow.com/questions/1945615/how-to-map-the-type-interval-in-hibernate/6139581#6139581
*
* @version 1.0
* @since 1.0
*/
public class IntervalType implements UserType {
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}
public boolean equals(Object arg0, Object arg1) throws HibernateException {
return arg0 != null && arg1 != null && arg0.equals(arg1) || arg0 == null && arg1 == null;
}
public int hashCode(Object object) throws HibernateException {
return object.hashCode();
}
@Override
public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object o) throws HibernateException, SQLException {
String interval = resultSet.getString(names[0]);
if (resultSet.wasNull() || interval == null) {
return null;
}
PGInterval pgInterval = new PGInterval(interval);
return getDuration(pgInterval);
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.OTHER);
} else {
//this http://postgresql.1045698.n5.nabble.com/Inserting-Information-in-PostgreSQL-interval-td2175203.html#a2175205
Duration duration = (Duration) value;
st.setObject(index, getInterval(duration), Types.OTHER);
}
}
public static Duration getDuration(PGInterval pgInterval) {
return Duration.ofSeconds(pgInterval.getDays() * 24 * 3600 +
pgInterval.getHours() * 3600 +
pgInterval.getMinutes() * 60 +
(int) pgInterval.getSeconds());
}
private static PGInterval getInterval(Duration value) {
long seconds = value.getSeconds();
int days = (int) (seconds / (24 * 3600));
seconds -= days * 24 * 3600;
int hours = (int) (seconds / 3600);
seconds -= hours * 3600;
int minutes = (int) (seconds / 60);
seconds -= minutes * 60;
seconds = Math.abs(seconds);
return new PGInterval(0, 0, days, hours, minutes, seconds);
}
public boolean isMutable() {
return false;
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
public Class returnedClass() {
return Duration.class;
}
public int[] sqlTypes() {
return new int[]{Types.OTHER};
}
}
...
@Entity
@TypeDef(name = "interval", typeClass = IntervalType.class)
public class PaperStatis implements Serializable {
...
@Column(name = "avg_duration")
@Type(type = "interval")
public Duration getAvgDuration() {
return this.avgDuration;
}
...
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有