Viiveek Sankar
Viiveek Sankar

Reputation: 63

How do i use db sys date in hibernate

I saw an example where people have suggested to use the below

<property name="lastActivityDate" type="timestamp" >        
    <column name="C2810_LAST_ACTIVITY_DATE" />
    <formula>select sysdate  from dual</formula>
 </property>

My war file doesnt deploy when i have the fomula tag , it says property mapping has wrong number of columns. But if i remove it erveything works fine.

I need a way to get the db sys date a column. Can any one help me?

Upvotes: 0

Views: 4607

Answers (1)

Naveen
Naveen

Reputation: 11

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.

Upvotes: 1

Related Questions