我遇到了一个奇怪的行为,希望hibernate尝试使用HQL查询更新Date字段...
在dao类中,我有这个查询
public void updateInterruttore(int idInterruttore, Date dateTime, boolean stato) {
Session session = getSession();
Transaction tx = session.beginTransaction();
logger.debug("Datetime is " + dateTime);
String update = "update Interruttore i set i.dateTime = :dateTime, i.stato = :stato where idInterruttore = :idInterruttore";
session.createQuery(update).setDate("dateTime", dateTime).setBoolean("stato", stato)
.setInteger("idInterruttore", idInterruttore).setLockOptions(LockOptions.UPGRADE).executeUpdate();
tx.commit();
}
例如,记录器说我
Datetime is Sun Nov 20 23:31:23 CET 2016
但是在数据库中我有
20/11/2016 - 00:00:00
模型是
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_time")
@DateTimeFormat(pattern="dd/MM/yyyy - hh:mm")
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
,DB字段是TIMESTAMP,MySql DB
我以这种方式创建DateTime
DateTime date = new DateTime();
Date now = date.toDate();
我也尝试过这种方式,结果相同
Date date = new Date();
Date now = new TimeStamp(date.getTime());
橙色的东西我正在以正确的方式更新应用程序中的其他Date对象......