[英]Map MySql datetime to hibernate
I have a datetime
field in my MySql database. 我的MySql数据库中有一个
datetime
字段。 A typical entry looks like this: 2015-08-26 11:45:48
. 典型条目如下:
2015-08-26 11:45:48
。
But by the time it reaches my app the hours, minutes and seconds are always 00:00:00
. 但是当它到达我的应用程序时,小时,分钟和秒始终是
00:00:00
。
This is the mapping entry: 这是映射条目:
<property name="listingTime" column="listing_time"/>
The field in my Java
class is 我的
Java
类中的字段是
private java.sql.Timestamp startTime;
I tried setting various types for the listingTime
property in my hibernate.cfg.xml
but it doesn't change the all zeroes. 我尝试在
hibernate.cfg.xml
为listingTime
属性设置各种类型,但它不会更改全零。 What am I missing? 我错过了什么?
Better use annotations like this: 更好地使用这样的注释:
...
@Column
@Type(type="timestamp")
private Date listing_time;
OR you can do it this way, and if you don't want it to change see the "updateable" 或者你可以这样做,如果你不想改变它,看看“可更新”
@Column(name = "created", nullable = false, updatable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date created;
With JPA, Use temporal. 使用JPA,使用时间。
<entity class="com.package.of.my.entities.MyEntity" name="MyEntity">
<attributes>
<basic name="startTime">
<temporal>TIMESTAMP</temporal>
</basic>
</attributes>
</entity>
With Hibernate (hbm.xml), User 使用Hibernate(hbm.xml),用户
<hibernate-mapping package="com.package.of.my.entities">
<class name="MyEntity">
<timestamp name="startTime" column="START_TIME" />
</class>
</hibernate-mapping>
Or in annotation 或者在注释中
@Basic
@Temporal(TemporalType.TIMESTAMP)
private java.sql.Timestamp startTime;
Alternatively is to use Long datatype to store your timestamp. 或者使用Long数据类型来存储时间戳。 So, using annotation, you declare in your entity :
因此,使用注释,您在实体中声明:
@Basic
private Long startTime;
public void setStartTime(Long startTime) {
this.startTime = startTime;
}
public Long getStartTime() {
return startTime;
}
and in your logic, you do 在你的逻辑中,你做到了
Date dateStartTime = new Date();
entity.setStartTime(dateStartTime.getTime());
To set the date property...and 设置日期属性...和
Date dateStartTime = new Date(entity.getStartTime());
To get the date property. 获取日期属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.