简体   繁体   中英

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:

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. 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. 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. 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.

I solved it by generating the timestamp from 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)

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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