0

while updating datetime field using new Date() function, it having date part but it time part will be (00.00.00), Insertion it does,t have any problem 2013-08-02 12:57:09 ,but in updation it works like 2013-08-02 00:00:00....

session.createQuery(
"update DeviceDetails u set u.statusUpDate=:statusUpDate where u.id=:dID")
.setDate("statusUpDate", new Date())
.setInteger("dID",dID).executeUpdate();

session.getTransaction().commit()
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
Aravind Cheekkallur
  • 3,157
  • 6
  • 27
  • 41

1 Answers1

3

Use setTimestamp(), and not setDate().

Or don't use a query at all: get the entity using Session.get(), and modify its fields.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Why are you repeating the code you already have in your question in this comment? – JB Nizet Aug 02 '13 at 09:31
  • I am really interest to know the difference between setDate(),setTimestamp(). – Aravind Cheekkallur Aug 20 '13 at 06:23
  • setDate() uses a java.sql.Date (which doesn't have a time portion), whereas setTimestamp() uses a java.sql.Timestamp (which has a time portion). Read the javadoc of those two classes to know the exact difference. – JB Nizet Aug 20 '13 at 06:28
  • AM using hibernate query to save the date and time...It working perfectly on saving the session object checkAdd.setcheck(newDate()); session.save(checkAdd); session.getTransaction().commit(); Once using update it working with date part not in time part... Actually the problem solved by using setTimestamp() function.Is the mentioned problem occurs because of the java library file – Aravind Cheekkallur Aug 20 '13 at 06:58
  • The problem occurs because you're using setDate(), which means: set the value of the parameter with this date without time value. – JB Nizet Aug 20 '13 at 07:31