如何将休眠时间戳映射到MySQL BIGINT?
I am using Hibernate 3.x, MySQL 4.1.20 with Java 1.6. I am mapping a Hibernate Timestamp to a 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. I figure I can use a BIGINT instead of TIMESTAMP in my table and convert the types in my Java code. 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 3.x,MySQL 4.1.20和Java 1.6。我正在将Hibernate Timestamp映射到MySQL TIMESTAMP。到现在为止还挺好。问题是MySQL以秒为单位存储TIMESTAMP并丢弃毫秒,现在我需要毫秒精度。我想我可以在我的表中使用BIGINT而不是TIMESTAMP并转换我的Java代码中的类型。我试图弄清楚是否有更好的方法使用hibernate,mysql,JDBC或某种组合这样做,所以我仍然可以在我的HSQL和/或SQL查询中使用日期函数?
4 个解决方案
#1
3
Also, look at creating a custom Hibernate Type implementation. Something along the lines of (psuedocode as I don't have a handy environment to make it bulletproof):
另外,请看创建自定义Hibernate Type实现。 (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. If you are using Hibernate annotations, it be along the lines of:
然后,您需要使用hibernate TypeDef和Type映射来映射新对象。如果您正在使用Hibernate注释,它将遵循以下方式:
@TypeDef (name="bigIntCalendar", typeClass=CalendarBigIntType.class)
@Entity
public class MyEntity {
@Type(type="bigIntCalendar")
private Calendar myDate;
}
更多相关文章
- 谁能帮忙注释一下这个js程序的功能?最好每一句都解释一下
- C#Razor - 显示注释值,其中循环值等于selectedIndex值
- Python2基础用法(中文编码,标识符,注释)
- 关于Python的编码注释# -*- coding:utf-8 -*- 详解
- day05 python程序中的注释
- 漂亮的LinuxC注释转换器--(2.2)目录级联转换最终实现
- Linux 常用命令汇总--加了注释
- 如何使用来自其他模型的信息来注释查询集,或者在Django Rest Fram
- PowerDesigner 生成SQL Server 2005 注释脚本