-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Insert new record - Date field with current time
PostPosted: Tue May 04, 2004 7:40 am 
Newbie

Joined: Thu Apr 22, 2004 11:47 pm
Posts: 4
Are there any good way to insert record using Session.save, so that the date field can be set to current timestamp relative to Database?

Normal SQL for Sybase,
insert into test_table (field1, field2, cdate) VALUES ('', '', GETDATE())

I am thinking of Session.createSQLQuery() instead of Session.save(), but it seems not good to write the whole insert-hsql for that.

Any better idea? Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 7:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
y'know, I am kinda inclined to put this on the TODO list. What do other people think?

Code:
<property name="insertDate"
    column="CREATE_DATE"
    insert-formula="GETDATE()"
    update="false"/>


The only trouble - and the reason I never did it until now - is that you would need to refresh() the object to see the generated value. But in many usecases that would simply not be such a big deal...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 7:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Code:
<property name="insertDate"
    column="CREATE_DATE"
    insert-javaUtilMethod="hibernate.util.getDate()"
    update="false"/>


it could prevent from refresh if the method is called before the insert.
And few methods could also be written....

What do you think of this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 7:50 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
-1

While handy, the mandatory sequence of API calls is not expected if you add a mapping artefact and is (with the exception of assigned identifiers) the only place where a mapping would mandate a specific call sequence. +1 if Hibernate refreshes the object automatically, then we only have to warn that it is bad for performance.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 9:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
This goes to another, unrelated discussion had on the forum a while ago, but another option would be to add the ability to retreive the current date/time from the underlying database either through Session or SessionFactory. That approach would put a little more onous on the user, as they would explicity have to make the API call to get the current date/time, but would get around the need to refresh. Plus it could be useful for other purposes as well.

I would guess this would need to be added to various dialects, but the base dialect could use the sql extension function now() which should work on the majority of platforms.

This could even be taken a step further, where the user could somehow in the mapping (maybe as a type) specify to grab the current date/time using this mechanism for insertions (and possibly even updates).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 6:20 pm 
Newbie

Joined: Thu Jun 17, 2004 6:11 pm
Posts: 1
I realize this topic is a couple of months old now, but I think this should be supported somehow. It is a pretty common idiom for tables to have created time and modified time columns where the time comes from the database. I understand the desire to avoid having to refresh the data back after an update, but this decision should be put in the user's hands IMHO. A generic mechanism might allow any computed columns and the ability to optionally refresh those. This could also allow the retrieval of column values updated by triggers.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 18, 2004 11:57 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
steve wrote:
This goes to another, unrelated discussion had on the forum a while ago, but another option would be to add the ability to retreive the current date/time from the underlying database either through Session or SessionFactory.


You should be able to do this now. Create a view that returns a single row containing the current date. In Oracle, it could look something like:

CREATE VIEW GET_CURRENT_DATE (CURRENT_DATE) AS
SELECT SYSDATE FROM DUAL;

The only issue that I can think of may be if Hibernate caches the object returned from an initial query and subsequent queries only return the object from the cache and doesn't requery the view in the database. However, the documentation does say that the various Session.find methods will never return stale data, so it sounds like it should work. (Somebody please correct me if I'm wrong).

_________________
Maury


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.