0

I don't understood the error This is error

    Stacktrace:] with root cause
com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'com.model.User@67f56542' for column 'intime' at row 1

Constructor

public User(Date intime) {
    super();
    this.intime = intime;
}

This is code

User user = new User(new Date());
    Configuration cfg = new Configuration();
    cfg.configure("com/resources/config.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session ss = sf.openSession();
    Transaction tx = ss.beginTransaction();
    Query q = ss.createQuery("update User set intime = '"+user+"' where email='"+email+"' ");
    q.executeUpdate();
    tx.commit();
    ss.close();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sandip Gadade
  • 199
  • 1
  • 2
  • 6
  • 2
    First and foremost, your code is SQL injectable. Almost _never_ should you concatenate the query parameters directly into the sql statement, but instead you should make use of prepared statements. That being said, your issue is that your are passing a `user` object for a `DATETIME` field, which is nonsense to an sql server. – Rogue Jul 11 '22 at 11:28
  • I have used hibernate – Sandip Gadade Jul 11 '22 at 12:00
  • How to solve this problem @Rogue – Sandip Gadade Jul 11 '22 at 12:16
  • Firstly, note that by concatenating `user` to the query, you've effectively called `User#toString` on that object, which will not output the value you want. The solution is to properly pass the `Date` field from the `User`, and ideally doing so through a [prepared statement in Hibernate](https://stackoverflow.com/questions/26405116/how-to-use-prepared-statement-in-a-hibernate-query) (which it does by default, you just have to simply use it). – Rogue Jul 11 '22 at 12:32

0 Answers0