Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

save entity with SYSDATE

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic