Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save entity with SYSDATE

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have Hibernate entity class User that contain "statusDate",
When I persist the object to the DB I do:

So I am reading the date & time from the local machine.
Is there any way I can update the statusDate with oracle's date and time?
I want to avoid native query like:


Thank you
Shaon
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are two ways. One is on the database side, where you set a default value to that field or you create an insert trigger that populates that value.

On the Hibernate side you can set the field/attribute to have a default value of "SYSDATE" or set the field/attribute to be a formula field.

My preferred method is to have the database field have a default value in the database side, and make the attribute mapping show the attribute as read-only. Which affectively makes only inserts update the field based on the database's default value, and Hibernate will not try to alter it at all, but will get that information when queried.

Mark
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand using default value is problematic:
http://www.andromda.org/jira/browse/HIB-13

I guess I will have to use trigger...

Thank you
Sharon
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sharon whipple:
As I understand using default value is problematic:
http://www.andromda.org/jira/browse/HIB-13

I guess I will have to use trigger...

Thank you
Sharon


I didn't see anything like that in that link. That link is also over 3-4 years old, so it is old data that isn't valid today.

I have only ever heard one DBA ever say that it is bad, and well, they said some other things that were so off base. I think that was true long ago, but not in today's databases. I worked on a project with 3000 tables and we used default values extensively and there was never any problem.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I am confusing over what you are now defining as "default" value. When I say "default" value I am talking about the database side, in the CREATE DATABASE command. where the in the field declaration it defines the default value for that field.

Where you might be defining "formula" attributes in Hibernate as "default".

Mark
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the default value according to this article:
http://blog.tremend.ro/2007/08/27/hibernate-annotations-default-value/

the field in the database seem to be created ok as you can see below,
but the field wont update to sysdate on update,
default values fields are relevant only on insert..

So i guess trigger is the correct option?




in Car class:
 
Naveen Dodla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,
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.

Please let me know if you have any concerns.

Thanks & Regards

Naveen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic