简体   繁体   English

TemporalType.TIMESTAMP白色java.util.Date不起作用(休眠版本3.2.7.ga)

[英]TemporalType.TIMESTAMP whit java.util.Date don't function (hibernate version 3.2.7.ga)

I have this in a class 我上课了

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "data_creazione")
private Date dataCreazione;

the object is saved if i put null on db on data_creazione I do: 如果我将data_creazione上的db放在null上,则保存对象,我这样做:

Date data=new Date();
newAccordo.setDataCreazione(data);

and also instead: 并且相反:

newAccordo.setDataCreazione(new Timestamp(data.getTime()));

but the object don't be saved. 但不会保存该对象。 I look for what put in query and is '2014-07-11 16:40:04' that is a normal date time. 我寻找查询中的内容,并且是“ 2014-07-11 16:40:04”,这是正常的日期时间。 if i put it manualy it work. 如果我手动将其工作。 in the db I try to put datetime and timestamp but don't write inside db anyway. 在数据库中,我尝试放置日期时间和时间戳,但无论如何不要在数据库中编写。

if i put: 如果我放:

@Temporal(TemporalType.TIME)

he write in db the date and the time, but after when I stamp it in jsp I see only right time and the date from util.date start. 他在db中写了日期和时间,但是当我在jsp中标记日期和时间后,我只能从util.date开始看到正确的时间和日期。 so i can't use this escamotage. 所以我不能使用这个骗局。 I think the problem can be in hibernate version. 我认为问题可能出在休眠版本中。

what do you think? 你怎么看?

You need to do this: 您需要这样做:

Date data=new Date();
newAccordo.setDataCreazione(new Timestamp(data.getTime());  // data --> date ??

Because the column type is Timestamp (yyyy-mm-dd HH:mm:ss.), not Date(yyyy-mm-dd) type. 因为列类型是时间戳(yyyy-mm-dd HH:mm:ss。),而不是Date(yyyy-mm-dd)类型。 Make sure you import 确保您导入

import java.sql.Timestamp;
import java.util.Date;

Also set your hibernate to <property name="hbm2ddl.auto">update</property> , which will automatically update the column data type. <property name="hbm2ddl.auto">update</property>休眠设置为<property name="hbm2ddl.auto">update</property> ,这将自动更新列数据类型。

I solved it by generating the timestamp from mysql. 我通过从mysql生成时间戳来解决它。 anyway i think is a bug of the old hibernate version (3.2.7.ga) that I'm using or of the Annotation version (3.3.0.ga) 无论如何,我认为这是我正在使用的旧休眠版本(3.2.7.ga)或Annotation版本(3.3.0.ga)的错误

anyway the model is 无论如何,模型是

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "data_creazione")
private Date dataCreazione;

public Date getDataCreazione() {
    return dataCreazione;
}

public void setDataCreazione(Date dataCreazione) {
    this.dataCreazione = dataCreazione;
}

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

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