简体   繁体   English

如何使用hql在MySql中设置datetime字段

[英]How to set datetime field in MySql using hql

I am facing an issue in inserting date in YYYY-mm-dd hh:mm:ss format in MySql DB. 我在MySql DB中以YYYY-mm-dd hh:mm:ss格式插入日期时遇到问题。 filed in DB is of type datetime. 在DB中提交的是datetime类型。

I tried it : 我尝试过这个 :

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.MINUTE, startMinute);
calendar.set(Calendar.SECOND,01);
Date startTime = calendar.getTime();

String hqlUpdate = "update challenge set start_time = :startTime where challenge_id =  :id";
sessionFactory.getCurrentSession().createSQLQuery(hqlUpdate)
                                 .setDate("startTime", startTime)
                                 .setInteger("id", id).executeUpdate();

尝试使用以下内容。

.setTimestamp("startTime", startTime);

Here I have specifically tried to format the date object using simpleDateFormat before passing it to the hibernate. 在这里,我特意尝试使用simpleDateFormat格式化日期对象,然后再将其传递给hibernate。 check if this works. 检查这是否有效。

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.MINUTE, startMinute);
calendar.set(Calendar.SECOND,01);
Date startTime = dateFormat.parse(dateFormat.format(calendar.getTime();));

String hqlUpdate = "update challenge set start_time = :startTime where challenge_id =  :id";
sessionFactory.getCurrentSession().createSQLQuery(hqlUpdate)
                             .setDate("startTime", startTime)
                             .setInteger("id", id).executeUpdate();

如果使用setParameter()而不是setDate()它对我setParameter() ...

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

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