I am using Hibernate for ORM mapping. One of the tables has a column of type "datetime". The column in question needs to be updated with "current-time" (time of data insertion). I am aware that I can use the date() function as the default value. I would rather prefer setting the timestamp when I set other attributes of the object.

Question 1) What is the equivalent of SQL date() function in Java?

有帮助吗?

解决方案

The equivalent of SQL date() in java is simply new Date(), e.g.:

myEntity.setDateField(new Date());

will set dateField to the current date and time.

其他提示

With mysql you can set an on update change : http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html

CREATE TABLE t1 (
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

The attribut will be updated with the current timestamp of the DB.

(you are lucky because it doesn't work with oracle ... (and I am with oracle ...) )

I am trying in my code:

myEntity.setdDateCreated(new Date());

In hibernate mapping ->

property name="dateCreated" column="date_created" type="timestamp"

But in mysql it is saving as '0000-00-00 00:00:00'

Any guess why this is happening?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top