Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > hibernate: automatically generate timestamps

Reply
Thread Tools

hibernate: automatically generate timestamps

 
 
chrislewis
Guest
Posts: n/a
 
      10-22-2007
Hello,

I am trying to have certain values created/updated when a new record
is created or an existing one is updated. I'm using the following
field (using hibernate annotations as demonstrated in Hibernate In
Action):

@Column(updatable = false, insertable = false)
@org.hibernate.annotations.Generated(
org.hibernate.annotations.GenerationTime.INSERT
)
private Date created;

Its simple; all I want is the 'created' field to be provided by
hibernate as a timestamp for when it was created. Similarly, I want a
timestamp field (named 'modified') to be updated whenever the record
is changed. For that I have:

@Column(updatable = false, insertable = false)
@org.hibernate.annotations.Generated(
org.hibernate.annotations.GenerationTime.ALWAYS
)
private Date modified;

Unfortunately neither of these work. From the book as well as other
documentation (the website), I can't see that I'm doing anything
incorrectly, but the behavior I'm getting is that both of these fields
are always null. Can anyone see what I've done wrong (or not doing)?

Sincerely,
Chris

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      10-22-2007
chrislewis wrote:
> Hello,
>
> I am trying to have certain values created/updated when a new record
> is created or an existing one is updated. I'm using the following
> field (using hibernate annotations as demonstrated in Hibernate In
> Action):
>
> @Column(updatable = false, insertable = false)
> @org.hibernate.annotations.Generated(
> org.hibernate.annotations.GenerationTime.INSERT
> )
> private Date created;

Just a guess here, have you tried using insertable = true? I don't know
Hibernate that well though. The other (less preferred) alternative is
to have a getter that checks if created==null then return new Date(),
else return created. Then have hibernate persist using the getter, not
the field.

Good luck,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
chrislewis
Guest
Posts: n/a
 
      10-22-2007
If I use insertable=true, hibernate throws an exception saying:

"Cannot have @Generated property and insertable columns: ..." - so it
appears that you must use insertable=false with @Generated, and the
book (Hibernate In Action) states this as well. However it doesn't
work as the book says it should, unless I'm just misunderstanding it.
I know hibernate can do this - its a simple operation - I just can't
figure out how...

On Oct 22, 4:50 pm, Daniel Pitts
<(E-Mail Removed)> wrote:
> chrislewis wrote:
> > Hello,

>
> > I am trying to have certain values created/updated when a new record
> > is created or an existing one is updated. I'm using the following
> > field (using hibernate annotations as demonstrated in Hibernate In
> > Action):

>
> > @Column(updatable = false, insertable = false)
> > @org.hibernate.annotations.Generated(
> > org.hibernate.annotations.GenerationTime.INSERT
> > )
> > private Date created;

>
> Just a guess here, have you tried using insertable = true? I don't know
> Hibernate that well though. The other (less preferred) alternative is
> to have a getter that checks if created==null then return new Date(),
> else return created. Then have hibernate persist using the getter, not
> the field.
>
> Good luck,
> Daniel.
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



 
Reply With Quote
 
Charles
Guest
Posts: n/a
 
      10-22-2007
On Oct 22, 8:12 am, chrislewis <(E-Mail Removed)> wrote:
> Hello,
>
> I am trying to have certain values created/updated when a new record
> is created or an existing one is updated. I'm using the following
> field (using hibernate annotations as demonstrated in Hibernate In
> Action):
>
> @Column(updatable = false, insertable = false)
> @org.hibernate.annotations.Generated(
> org.hibernate.annotations.GenerationTime.INSERT
> )
> private Date created;
>
> Its simple; all I want is the 'created' field to be provided by
> hibernate as a timestamp for when it was created. Similarly, I want a
> timestamp field (named 'modified') to be updated whenever the record
> is changed. For that I have:
>
> @Column(updatable = false, insertable = false)
> @org.hibernate.annotations.Generated(
> org.hibernate.annotations.GenerationTime.ALWAYS
> )
> private Date modified;
>
> Unfortunately neither of these work. From the book as well as other
> documentation (the website), I can't see that I'm doing anything
> incorrectly, but the behavior I'm getting is that both of these fields
> are always null. Can anyone see what I've done wrong (or not doing)?
>
> Sincerely,
> Chris


Hi,

Can you give an example of the time value being created?

Complete prototype of the time structure would be helpful.


 
Reply With Quote
 
chrislewis
Guest
Posts: n/a
 
      10-22-2007
Its not, thats the problem. The Java type is java.util.Date, and
hibernate is translating this to a SQL DATETIME column, which is
perfect. The schema comes out fine, but anytime I create a new object
with a 'created' field annotated as aforementioned, it always comes up
null in the database. The expected behavior here is that hibernate
should see it needs to initialize that field transparently on insert.

I'm using MySQL 5x, for which the DATETIME format is 'YYYY-MM-DD
HH:MM:SS'.

Thanks for any input!

On Oct 22, 5:22 pm, Charles <(E-Mail Removed)> wrote:
> On Oct 22, 8:12 am, chrislewis <(E-Mail Removed)> wrote:
>
>
>
> > Hello,

>
> > I am trying to have certain values created/updated when a new record
> > is created or an existing one is updated. I'm using the following
> > field (using hibernate annotations as demonstrated in Hibernate In
> > Action):

>
> > @Column(updatable = false, insertable = false)
> > @org.hibernate.annotations.Generated(
> > org.hibernate.annotations.GenerationTime.INSERT
> > )
> > private Date created;

>
> > Its simple; all I want is the 'created' field to be provided by
> > hibernate as a timestamp for when it was created. Similarly, I want a
> > timestamp field (named 'modified') to be updated whenever the record
> > is changed. For that I have:

>
> > @Column(updatable = false, insertable = false)
> > @org.hibernate.annotations.Generated(
> > org.hibernate.annotations.GenerationTime.ALWAYS
> > )
> > private Date modified;

>
> > Unfortunately neither of these work. From the book as well as other
> > documentation (the website), I can't see that I'm doing anything
> > incorrectly, but the behavior I'm getting is that both of these fields
> > are always null. Can anyone see what I've done wrong (or not doing)?

>
> > Sincerely,
> > Chris

>
> Hi,
>
> Can you give an example of the time value being created?
>
> Complete prototype of the time structure would be helpful.



 
Reply With Quote
 
JDurbin
Guest
Posts: n/a
 
      10-29-2007
On Oct 22, 7:12 am, chrislewis <(E-Mail Removed)> wrote:
> Hello,
>
> I am trying to have certain values created/updated when a new record
> is created or an existing one is updated. I'm using the following
> field (using hibernate annotations as demonstrated in Hibernate In
> Action):
>
> @Column(updatable = false, insertable = false)
> @org.hibernate.annotations.Generated(
> org.hibernate.annotations.GenerationTime.INSERT
> )
> private Date created;
>
> Its simple; all I want is the 'created' field to be provided by
> hibernate as a timestamp for when it was created. Similarly, I want a
> timestamp field (named 'modified') to be updated whenever the record
> is changed. For that I have:
>
> @Column(updatable = false, insertable = false)
> @org.hibernate.annotations.Generated(
> org.hibernate.annotations.GenerationTime.ALWAYS
> )
> private Date modified;
>
> Unfortunately neither of these work. From the book as well as other
> documentation (the website), I can't see that I'm doing anything
> incorrectly, but the behavior I'm getting is that both of these fields
> are always null. Can anyone see what I've done wrong (or not doing)?
>
> Sincerely,
> Chris


Chris. Have you found out what the issue is with @Generated using a
create/modify timestamp? I am experiencing the exact-same thing you
are. All the examples and docs make it seem so cut-'n-dry... thus, I
cannot figure out what is going wrong.

Thanks!

--Josh

 
Reply With Quote
 
rwinch@gmail.com
Guest
Posts: n/a
 
      11-01-2007
On Oct 28, 11:40 pm, JDurbin <(E-Mail Removed)> wrote:
> On Oct 22, 7:12 am, chrislewis <(E-Mail Removed)> wrote:
>
>
>
> > Hello,

>
> > I am trying to have certain values created/updated when a new record
> > is created or an existing one is updated. I'm using the following
> > field (using hibernate annotations as demonstrated in Hibernate In
> > Action):

>
> > @Column(updatable = false, insertable = false)
> > @org.hibernate.annotations.Generated(
> > org.hibernate.annotations.GenerationTime.INSERT
> > )
> > private Date created;

>
> > Its simple; all I want is the 'created' field to be provided by
> > hibernate as a timestamp for when it was created. Similarly, I want a
> > timestamp field (named 'modified') to be updated whenever the record
> > is changed. For that I have:

>
> > @Column(updatable = false, insertable = false)
> > @org.hibernate.annotations.Generated(
> > org.hibernate.annotations.GenerationTime.ALWAYS
> > )
> > private Date modified;

>
> > Unfortunately neither of these work. From the book as well as other
> > documentation (the website), I can't see that I'm doing anything
> > incorrectly, but the behavior I'm getting is that both of these fields
> > are always null. Can anyone see what I've done wrong (or not doing)?

>
> > Sincerely,
> > Chris

>
> Chris. Have you found out what the issue is with @Generated using a
> create/modify timestamp? I am experiencing the exact-same thing you
> are. All the examples and docs make it seem so cut-'n-dry... thus, I
> cannot figure out what is going wrong.
>
> Thanks!
>
> --Josh


I am also having this problem and would be interested to hear an
answer if anyone has got it

 
Reply With Quote
 
Jim Garrison
Guest
Posts: n/a
 
      11-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Oct 28, 11:40 pm, JDurbin <(E-Mail Removed)> wrote:
>> On Oct 22, 7:12 am, chrislewis <(E-Mail Removed)> wrote:
>>
>>
>>
>>> Hello,
>>> I am trying to have certain values created/updated when a new record
>>> is created or an existing one is updated. I'm using the following
>>> field (using hibernate annotations as demonstrated in Hibernate In
>>> Action):
>>> @Column(updatable = false, insertable = false)
>>> @org.hibernate.annotations.Generated(
>>> org.hibernate.annotations.GenerationTime.INSERT
>>> )
>>> private Date created;
>>> Its simple; all I want is the 'created' field to be provided by
>>> hibernate as a timestamp for when it was created. Similarly, I want a
>>> timestamp field (named 'modified') to be updated whenever the record
>>> is changed. For that I have:
>>> @Column(updatable = false, insertable = false)
>>> @org.hibernate.annotations.Generated(
>>> org.hibernate.annotations.GenerationTime.ALWAYS
>>> )
>>> private Date modified;
>>> Unfortunately neither of these work. From the book as well as other
>>> documentation (the website), I can't see that I'm doing anything
>>> incorrectly, but the behavior I'm getting is that both of these fields
>>> are always null. Can anyone see what I've done wrong (or not doing)?
>>> Sincerely,
>>> Chris

>> Chris. Have you found out what the issue is with @Generated using a
>> create/modify timestamp? I am experiencing the exact-same thing you
>> are. All the examples and docs make it seem so cut-'n-dry... thus, I
>> cannot figure out what is going wrong.
>>
>> Thanks!
>>
>> --Josh

>
> I am also having this problem and would be interested to hear an
> answer if anyone has got it
>


I filed a bug on this in June, but have received zero response
from anybody at Hibernate. See
http://opensource.atlassian.com/proj...rowse/HHH-2511

Maybe if you would visit the bug and add your examples it might
get a little visibility.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timestamps in show log history Simon Cisco 7 10-20-2005 02:19 PM
Using SQL TimeStamps in datagrid Craig G ASP .Net 4 03-01-2005 12:42 AM
Component for logging timestamps - Need guidance from Gurus Chumma Dede ASP .Net 2 02-11-2005 03:18 PM
email timestamps? joseph white Firefox 1 12-28-2004 09:12 PM
Cisco Timestamps Rob@homenospam.com Cisco 1 02-24-2004 11:57 PM



Advertisments