简体   繁体   English

如何将休眠时间戳映射到MySQL BIGINT?

[英]How do I map a hibernate Timestamp to a MySQL BIGINT?

I am using Hibernate 3.x, MySQL 4.1.20 with Java 1.6. 我正在使用Hibernate 3.x,MySQL 4.1.20和Java 1.6。 I am mapping a Hibernate Timestamp to a MySQL TIMESTAMP. 我正在将Hibernate Timestamp映射到MySQL TIMESTAMP。 So far so good. 到现在为止还挺好。 The problem is that MySQL stores the TIMESTAMP in seconds and discards the milliseconds and I now need millisecond precision. 问题是MySQL以秒为单位存储TIMESTAMP并丢弃毫秒,现在我需要毫秒精度。 I figure I can use a BIGINT instead of TIMESTAMP in my table and convert the types in my Java code. 我想我可以在我的表中使用BIGINT而不是TIMESTAMP并转换我的Java代码中的类型。 I'm trying to figure out if there is a better way of doing this using hibernate, mysql, JDBC or some combination so I can still use date functions in my HSQL and/or SQL queries? 我试图弄清楚是否有更好的方法使用hibernate,mysql,JDBC或其他组合这样做,所以我仍然可以在我的HSQL和/或SQL查询中使用日期函数?

Also, look at creating a custom Hibernate Type implementation. 另外,请看创建自定义Hibernate Type实现。 Something along the lines of (psuedocode as I don't have a handy environment to make it bulletproof): 有点像(psuedocode,因为我没有方便的环境,使其防弹):

public class CalendarBigIntType extends org.hibernate.type.CalendarType {
    public Object get(ResultSet rs, String name) {
        return cal = new GregorianCalendar(rs.getLong(name));
    }
    public void set(PreparedStatement stmt, Object value, int index) {
        stmt.setParameter(index, ((Calendar) value).getTime());
    }
}

Then, you'll need to map your new object using a hibernate TypeDef and Type mappings. 然后,您需要使用hibernate TypeDef和Type映射来映射新对象。 If you are using Hibernate annotations, it be along the lines of: 如果您正在使用Hibernate注释,它将遵循以下方式:

@TypeDef (name="bigIntCalendar", typeClass=CalendarBigIntType.class)
@Entity
public class MyEntity {
    @Type(type="bigIntCalendar")
    private Calendar myDate;
}

For those who are still interested in this issue: MySQL 5.6.4 supports timestamps with precision. 对于那些仍然对此问题感兴趣的人:MySQL 5.6.4支持精确的时间戳。 Subclassing MySQL5Dialect to override the used MySQL type solves the problem. 对MySQL5Dialect进行子类化以覆盖使用的MySQL类型解决了这个问题。

I altered my datatyp from timestamp to decimal(17,3) and wrote some helper methods 我将我的数据类型从时间戳改为十进制(17,3)并编写了一些辅助方法

public static Calendar bigDec2Cal(BigDecimal tsp) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(tsp.longValue());
    return cal;
}

public static Date bigDec2Date(BigDecimal tsp) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(tsp.longValue());
    return cal.getTime();
}

public static BigDecimal cal2BigDec(Calendar cal) {
    BigDecimal tsp = new BigDecimal(cal.getTimeInMillis());
    return tsp;
}

public static BigDecimal date2BigDec(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    BigDecimal tsp = new BigDecimal(cal.getTimeInMillis());
    return tsp;
}

Why not use it in addition to the TIMESTAMP field? 除TIMESTAMP字段外,为什么不使用它? You would have one field (which is already defined) for storing the date, without the milliseconds, and another field for the milliseconds. 您将有一个字段(已定义)用于存储日期(没有毫秒),另一个字段用于毫秒。 You can still run your HSQL queries on the first field, except you will have to ensure that you take care of storing the millisecond properly (via parsing of your Java Date object before you store it using Hibernate). 您仍然可以在第一个字段上运行HSQL查询,除非您必须确保正确地存储毫秒(通过在使用Hibernate存储之前解析Java Date对象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM