Hello,I am trying to have certain values created/updated when a new recordis created or an existing one is updated. I'm using the followingfield (using hibernate annotations as demonstrated in Hibernate InAction): @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 byhibernate as a timestamp for when it was created. Similarly, I want atimestamp field (named 'modified') to be updated whenever the recordis 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 otherdocumentation (the website), I can't see that I'm doing anythingincorrectly, but the behavior I'm getting is that both of these fieldsare always null. Can anyone see what I've done wrong (or not doing)?Sincerely,Chris
|
|
0
|
|
|
|
Reply
|
burningodzilla (6)
|
10/22/2007 12:12:23 PM |
|
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/>
|
|
0
|
|
|
|
Reply
|
Daniel
|
10/22/2007 2:50:37 PM
|
|
If I use insertable=true, hibernate throws an exception saying:"Cannot have @Generated property and insertable columns: ..." - so itappears that you must use insertable=false with @Generated, and thebook (Hibernate In Action) states this as well. However it doesn'twork 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'tfigure out how...On Oct 22, 4:50 pm, Daniel Pitts<newsgroup.spamfil...@virtualinfinity.net> 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/>
|
|
0
|
|
|
|
Reply
|
chrislewis
|
10/22/2007 3:21:40 PM
|
|
On Oct 22, 8:12 am, chrislewis <burningodzi...@gmail.com> 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,> ChrisHi,Can you give an example of the time value being created?Complete prototype of the time structure would be helpful.
|
|
0
|
|
|
|
Reply
|
Charles
|
10/22/2007 3:22:51 PM
|
|
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 <charlesalo...@gmail.com> wrote:
> On Oct 22, 8:12 am, chrislewis <burningodzi...@gmail.com> 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.
|
|
0
|
|
|
|
Reply
|
chrislewis
|
10/22/2007 4:40:08 PM
|
|
On Oct 22, 7:12 am, chrislewis <burningodzilla@gmail.com> 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,> ChrisChris. Have you found out what the issue is with @Generated using acreate/modify timestamp? I am experiencing the exact-same thing youare. All the examples and docs make it seem so cut-'n-dry... thus, Icannot figure out what is going wrong.Thanks!--Josh
|
|
0
|
|
|
|
Reply
|
JDurbin
|
10/29/2007 4:40:00 AM
|
|
On Oct 28, 11:40 pm, JDurbin <durbinjo...@gmail.com> wrote:
> On Oct 22, 7:12 am, chrislewis <burningodzi...@gmail.com> 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
|
|
0
|
|
|
|
Reply
|
rwinch
|
11/1/2007 3:54:01 PM
|
|
rwinch@gmail.com wrote:
> On Oct 28, 11:40 pm, JDurbin <durbinjo...@gmail.com> wrote:
>> On Oct 22, 7:12 am, chrislewis <burningodzi...@gmail.com> 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/projects/hibernate/browse/HHH-2511
Maybe if you would visit the bug and add your examples it might
get a little visibility.
|
|
0
|
|
|
|
Reply
|
Jim
|
11/7/2007 5:26:45 AM
|
|
|
7 Replies
338 Views
(page loaded in 0.168 seconds)
Similiar Articles:7/24/2012 4:49:17 PM
|