What I did was add a BaseEntity from which I create my other entities:

import java.util.Date
import javax.persistence._

/**
   The base entity from which audited entities are extended
*/
@MappedSuperclass
class BaseEntity {

   @Temporal(TemporalType.TIMESTAMP)
   @Column{val name="CREATED_AT", val updatable = false}
   var createdAt : Date = new Date()

   @Temporal(TemporalType.TIMESTAMP)
   @Column{val name="UPDATED_AT"}
   var updatedAt : Date = new Date()

   @PrePersist
   def setCreatedAt = { createdAt = new Date; updatedAt = createdAt }

   @PreUpdate
   def setUpdatedAt = { updatedAt = new Date }

}

Works beautifully!

Chas.

Derek Chen-Becker wrote:
> It's a JPA annotation for lifecycle methods. There's a really nice chart 
> explaining when they get called here:
> 
> http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#CHDCHFJJ
> 
> Basically, for what Chas wants to do, I'd do something like
> 
> //define fields
> @Temporal{val value = TemporalType.TIMESTAMP}
> var createTime : Date = _
> 
> @Temporal{val value = TemporalType.TIMESTAMP}
> var updateTime : Date = _
> 
> @PrePersist
> def markCreateTime = { createTime = new Date; updateTime = createTime }
> 
> @PreUpdate
> def markUpdateTime = { updateTime = new Date }
> 
> 
> Derek
> 
> On Fri, Oct 24, 2008 at 9:27 AM, Tim Perrett <[EMAIL PROTECTED] 
> <mailto:[EMAIL PROTECTED]>> wrote:
> 
> 
>     Hey Derek,
> 
>     Whats @PrePersist - cant say im that familiar with it?
> 
>     Cheers
> 
>     Tim
> 
>     On Oct 24, 4:10 pm, "Derek Chen-Becker" <[EMAIL PROTECTED]
>     <mailto:[EMAIL PROTECTED]>> wrote:
>      > Personally I use the @PrePersist lifecycle method interceptor
>     when I want to
>      > do things like record dates. Not quite as concise as the
>     Hibernate stuff,
>      > but it's more flexible since you control the logic for what gets
>      > set/updated.
>      >
>      > Derek
>      >
>      > On Fri, Oct 24, 2008 at 5:16 AM, Tim Perrett
>     <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:
>      >
>      > > I think you should be able to do this by using an interceptor
>     or event
>      > > listener - does this article help:
>      >
>      > >http://java.dzone.com/articles/using-a-hibernate-interceptor-
>      >
>      > > If you manage to get the created_on and updated_at stuff
>     working I'd
>      > > be interested in how you did it as I think its something we
>     should be
>      > > using.
>      >
>      > > Cheers
>      >
>      > > Tim
>      >
>      > > On Oct 22, 9:38 am, "Viktor Klang" <[EMAIL PROTECTED]
>     <mailto:[EMAIL PROTECTED]>> wrote:
>      > > > After migrating from Hibernate 3.0 to 3.3.1 the past 2 weeks
>     I have
>      > > gained
>      > > > some hatred towards Hibernate.
>      >
>      > > > I had to write atleast 3 workarounds to Hibernate bugs. No
>     cool at all.
>      > > :/
>      >
>      > > > I feel your pain,
>      >
>      > > > cheers,
>      > > > Viktor
>      >
>      > > > On Wed, Oct 22, 2008 at 1:41 AM, Charles F. Munat
>     <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>
>      > > wrote:
>      >
>      > > > > Nope. Turns out this is a really poorly explained "feature" of
>      > > Hibernate
>      > > > >  (and, in the opinion of many, a really dumb one). My code
>     was correct,
>      > > > > and the problem isn't anything Scala-related. In order for
>     this code to
>      > > > > work, the *database* has to generate the values, e.g. via a
>     trigger,
>      > > > > which I, the designer, have to add.
>      >
>      > > > > Sheesh. The whole point of Hibernate, I thought, was that I
>     don't have
>      > > > > to deal with the database end. Why this can't just add the
>     triggers for
>      > > > > me is beyond me. It's easier just to set them in the
>     application, I
>      > > think.
>      >
>      > > > > Thanks for the help. Live and learn, I guess.
>      >
>      > > > > Chas.
>      >
>      > > > > Derek Chen-Becker wrote:
>      > > > > > Yeah, I think you want insertable to be true on the first
>     one (just
>      > > omit
>      > > > > > the insertable val) and on the second one you want to
>     omit both
>      > > > > > insertable and updatable to make them both true.
>      >
>      > > > > > Derek
>      >
>      > > > > > On Tue, Oct 21, 2008 at 6:00 AM, Viktor Klang <
>      > > [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
>      > > > > > <mailto:[EMAIL PROTECTED]
>     <mailto:[EMAIL PROTECTED]>>> wrote:
>      >
>      > > > > >     But both are updatable false and insertable false?
>      >
>      > > > > >     I might be daft, but that doesn't look good to me...
>      >
>      > > > > >     Cheers
>      > > > > >     Viktor
>      >
>      > > > > >     On Tue, Oct 21, 2008 at 5:06 AM, Charles F. Munat <
>      > > [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
>      > > > > >     <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>> 
> wrote:
>      >
>      > > > > >         In my Lift app based on the JPA demo I tried
>     this, which
>      > > should
>      > > > > work
>      > > > > >         beautifully according to everything I've been
>     able to get my
>      > > > > >         hands on:
>      >
>      > > > > >         @Temporal(TemporalType.TIMESTAMP)
>      > > > > >         @Column{val name="CREATED_AT", val updatable = false,
>      > > > > >           val insertable = false}
>      >
>      > >
>     
> @org.hibernate.annotations.Generated(org.hibernate.annotations.GenerationTi
>      > > me.INSERT)
>      > > > > >         var createdAt : Date = new Date()
>      >
>      > > > > >         @Temporal(TemporalType.TIMESTAMP)
>      > > > > >         @Column{val name="UPDATED_AT", val updatable = false,
>      > > > > >           val insertable = false}
>      >
>      > >
>     
> @org.hibernate.annotations.Generated(org.hibernate.annotations.GenerationTi
>      > > me.ALWAYS)
>      > > > > >           var updatedAt : Date = new Date()
>      >
>      > > > > >         This should, if I'm right, set an immutable
>     created_at
>      > > timestamp
>      > > > > >         and a
>      > > > > >         mutable updated_at timestamp upon insert, and
>     update the
>      > > > > updated_at
>      > > > > >         timestamp upon each update.
>      >
>      > > > > >         What it actually does is leave both fields null.
>     What a drag.
>      >
>      > > > > >         Any ideas? Is this a Scala thing? Am I missing
>     something
>      > > really
>      > > > > >         obvious,
>      > > > > >         as usual?
>      >
>      > > > > >         Thanks,
>      >
>      > > > > >         Chas.
>      >
>      > > > > >     --
>      > > > > >     Viktor Klang
>      > > > > >     Senior Systems Analyst
>      >
>      > > > --
>      > > > Viktor Klang
>      > > > Senior Systems Analyst
> 
> 
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to