简体   繁体   English

使用休眠模式更新mysql中的DateTime类型

[英]Update DateTime type in mysql using hibernate

I have a DateTime field in my database. 我的数据库中有一个DateTime字段。 I am using hibernate and spring mvc framework. 我正在使用hibernatespring mvc框架。 I am able to insert DateTime field.If I update the DateTimeFiled then hour, min and seconds are set to zero. 我可以插入DateTime字段。如果我更新DateTimeFiled,则将小时,分钟和秒设置为零。

Here is my code: 这是我的代码:

    java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd  
    HH:mm:ss");
    String formattedDate = cd.getStartTimeFrom(); 

    System.out.println("input for dao date is "+formattedDate);

    Date tmp= new Date();
    try {
    tmp= sdf1.parse(formattedDate);
    } catch (ParseException e) {
    //  TODO Auto-generated catch block
   e.printStackTrace();
 }


       Configuration configuration = new Configuration().configure();
       ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
       registry.applySettings(configuration.getProperties());
       ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
       SessionFactory sessionFactory =             
       configuration.buildSessionFactory(serviceRegistry);
       Session session = sessionFactory.openSession();
       session.beginTransaction();
       Query query=session.createQuery("update FeedFile set   
       priority=:priority, startTime=:startTime where id=:id");
       query.setInteger("id", cd.getId());
       query.setDate("startTime",tmp);
       System.out.println("---- Priority : "+ cd.getPriority());
       query.setInteger("priority", cd.getPriority());
       modifications = query.executeUpdate();
       session.getTransaction().commit();

If I have the record with value 2015-11-19 03:05:22 and if I update the date value using above code to 2015-11-25 09:06:12 then op is 2015-11-25 00:00:00 如果我有记录值2015-11-19 03:05:22并且如果我使用上述代码将日期值更新为2015-11-25 09:06:12则op为2015-11-25 00:00:00

use in your class definition 在类定义中使用

@Temporal(TemporalType.TIMESTAMP)
 private java.util.Date yourDateField;

or in your class.hbm.xml file 或在您的class.hbm.xml文件中

<property column="yourDateInDB" name="yourDateInJava" type="timestamp"/>

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

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