开发者

How to insert/update with sysdate in hibernate?

i am using hibernate. db is oracle. my requirement is i need to insert/update a entity with sysdate but not new Date开发者_如何转开发(). please help me how to achieve this?

Thanks!


You can use current_timestamp() HQL function to achieve that.


After a long R&D, at last I found the solution for inserting/ updating with database SYSDATE (not application server Date) using hibernate in date column. Hope it will help you, please find the solution for the above

1) add extra property in mapping.hbm.xml file like below

property name="systemDate" formula="(select sysdate from dual)"

2) add setters and getters for the 'systemDate' property in POJO class

private Date systemDate;

public Date getSystemDate() {
    return this.systemDate;
}

public void setSystemDate(Date systemDate) {
    this.systemDate = systemDate;
}

3) In your DAO, before inserting or updating the date column, fetch the systemDate property and save the session with that value.

POJOclass Obj = new

// the below line returns the database system date,because we had given the formula for this property in mapping.xml file

Date systemDate= POJOclassObj.getSystemDate(); 

and add the above Date to your columns. It will insert Database sysdate.


The first (accepted) answer actually doesn't meet the requirement, as it will use new Date() under the covers. The OP's solution works, but it's a bit cumbersome. When the row is created, sysdate can be specified as the column default. For updates, most DBA's seem to define a before trigger that updates the column value. The trigger is beneficial because it prevents circumvention of the requirement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜