-->
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.  [ 8 posts ] 
Author Message
 Post subject: Generated Timestamp with Hibernate 3.1
PostPosted: Fri Jan 20, 2006 9:38 am 
Newbie

Joined: Thu Jan 19, 2006 6:28 pm
Posts: 4
I am trying to use the Hibernate 3.1 feature to generate timestamps on insert and update. Of the five tables I am using this on, three work and two do not. The coding of the attribute is exactly the same, even copied and pasted from one to the other.

I am using UDB 8.x and I get a SQL code 407, which means an insert was attempts with null for a non-null column.

This feature is very convenient and I would love to get it to work. However, three out of five just won't do it for me.

Here is how the property is coded:

<property name="chgOnDt" type="timestamp" column="CHG_ON_DT" generated="always" not-null="true"/>


Thanks,

Jules


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 6:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
generated means that the value is being generated *by the database*. Thus you'd need a trigger, etc actually setting these values. Sounds more like what you want is a <timestamp/> mapping.


Top
 Profile  
 
 Post subject: Generated Timestamp with Hibernate 3.1
PostPosted: Wed Jan 25, 2006 12:48 pm 
Newbie

Joined: Thu Jan 19, 2006 6:28 pm
Posts: 4
Thanks, Steve,

That gets me part way there. However, I can't discern any difference between saying generated="insert" and generated="always". The only effect of using these clauses appears to be that the column is omitted from the insert and update statements. When using generated="always", I would expect it to update using the DEFAULT value. This will update the value to the current timestamp if that was specified as the default value. Otherwise, the timestamp will only be generated on insert. A trigger would be needed to set it on update. While this is not impossible, it seems needless extra trouble if the generated="always" took care of it.

Regards,

Jules


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 3:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
sigh... <timestamp source="db"/>


Top
 Profile  
 
 Post subject: Generated Timestamp with Hibernate 3.1
PostPosted: Wed Jan 25, 2006 5:10 pm 
Newbie

Joined: Thu Jan 19, 2006 6:28 pm
Posts: 4
Sorry to try your patience. We definitely do NOT want to use optimistic locking. We want "last wins". Will using the Timestamp tag allow this? Is there anything special that I have to put in the .cfg.xml file?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 11:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well I'm just answering your questions as they come up. Quicker to provide all infor up front; we'll get the same result either way, just takes longer ;)

Anyway. Use of <timestamp/> will enforce optimistic locking semantics.

If you simply want "incrementing" timestamps for inserts/updates you basically have two options:

1) use a db trigger to write the timestamp and then map the property using generated="always". The difference between always/insert is simply when the database (aka trigger here) is generating these values; is it only on insert (generated="insert") or on both insert and update (generated="always")

2) use a Hibernate Interceptor to populate these values as they are being written to the database. The wiki already contains a few examples of this functionality.


Top
 Profile  
 
 Post subject: Generated Timestamp with Hibernate 3.1
PostPosted: Thu Jan 26, 2006 11:45 am 
Newbie

Joined: Thu Jan 19, 2006 6:28 pm
Posts: 4
Thanks for you help. They are also averse to using triggers here so I am going to use the <sql-update> tag. I have tried it and it works well. I think that this is easier than the Interceptor mechanism you mentioned.

I still maintain that the generated attribute is implemented badly as there is absolutely no discernable difference between "insert" and "always".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
i have a feeling thats because you do not at all understand what its purpose is ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.