在JPA / Hibernate中对java.util.Date或java.time.Instant使用BIGINT列

时间:2012-11-30 14:22:56

标签: java hibernate jpa

我需要访问使用BIGINT列存储时间戳的现有MySQL数据库:

create table mytable (created bigint);

现在我更喜欢使用java.util.Datejava.time.Instant实例而不是整数,所以我试图让Hibernate直接转换值。不幸的是,Hibernate在注释时不会识别该列:

@Column(name = "created")
private Date created;

或者像这样:

@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Calendar created;

这确实会在将来返回2017-07-01T04:14:00+02:00。如何正确地使Hibernate转换BIGINT列,以便我不必在getter和setter中转换它们?

1 个答案:

答案 0 :(得分:1)

Hibernate' TemporalType.TIMESTAMP映射到java.sql.TimestampTimestamp构造函数采用Long值,这是自纪元以来的毫秒数。数据库中的值存储为Unix时间戳,这是自纪元以来的秒数。

我写了一个UnixTimestampType,需要几秒钟而不是毫秒,并创建Date个实例:

public class UnixTimestampType extends AbstractSingleColumnStandardBasicType<Date> implements IdentifierType<Date>, LiteralType<Date> {
    private static final long serialVersionUID = 1L;
    public static final UnixTimestampType INSTANCE = new UnixTimestampType();

    public UnixTimestampType() {
        super(UnixTimestampTypeDescriptor.INSTANCE, JdbcDateTypeDescriptor.INSTANCE);
    }

    @Override
    public String getName() {
        return "date";
    }

    @Override
    public String[] getRegistrationKeys() {
        return new String[] { getName(), java.sql.Date.class.getName() };
    }

    @Override
    public String objectToSQLString(Date value, Dialect dialect) throws Exception {
        final java.sql.Date jdbcDate = java.sql.Date.class.isInstance(value) ? (java.sql.Date) value : new java.sql.Date(value.getTime());
        return StringType.INSTANCE.objectToSQLString(jdbcDate.toString(), dialect);
    }

    @Override
    public Date stringToObject(String xml) {
        return fromString(xml);
    }
}

public class UnixTimestampTypeDescriptor implements SqlTypeDescriptor {
    private static final long serialVersionUID = 1L;
    public static final UnixTimestampTypeDescriptor INSTANCE = new UnixTimestampTypeDescriptor();

    @Override
    public int getSqlType() {
        return Types.INTEGER;
    }

    @Override
    public boolean canBeRemapped() {
        return true;
    }

    @Override
    public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
        return new BasicBinder<X>(javaTypeDescriptor, this) {
            @Override
            protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
                Date date = javaTypeDescriptor.unwrap(value, Date.class, options);
                date.setTime(date.getTime() / 1000);
                st.setDate(index, date);
            }
        };
    }

    @Override
    public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
        return new BasicExtractor<X>(javaTypeDescriptor, this) {
            @Override
            protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
                Date date = new Date(rs.getLong(name) * 1000);
                return javaTypeDescriptor.wrap(date, options);
            }
        };
    }
}