Thanks Miłosz. Yes, that worked. Guess I was reading the documentation
too rigidly.

But I think I'm going to stick with @PrePersist, @PreUpdate because I
also have a 'created' field which should be set once. I suppose I could
add a constraint or trigger to enforce this but I've got something
working now and I need to move on. Thanks again for the input. I'll note
it for future reference.


On Sun, 2011-10-16 at 14:58 +0200, Miłosz Tylenda wrote:
> Tim,
> 
> You can apply @GeneratedValue to a field which is generated by the database 
> even if the field is not a primary key. The following did work for me on 
> PostgreSQL:
> 
> create table gv_test(id bigserial, modified timestamp not null default 
> current_timestamp, vvv int)
> 
> @Entity
> @Table(name="gv_test")
> public class Message {
>     @Id
>     @GeneratedValue(strategy=GenerationType.IDENTITY)
>     private long id;
>     private Integer vvv;
>     @GeneratedValue(strategy=GenerationType.IDENTITY)
>     private Date modified;
> 
>     public Message() {
>     }
> 
>     public Message(Integer v) {
>         vvv = v;
>     }
> 
>     public long getId() {
>         return id;
>     }
>     
>     public void setId(long val) {
>         id = val;
>     }
> 
>     public Date getModified() {
>         return modified;
>     }
> 
>     public void setModified(Date modified) {
>         this.modified = modified;
>     }
> 
>     public Integer getVvv() {
>         return vvv;
>     }
> 
>     public void setVvv(Integer vvv) {
>         this.vvv = vvv;
>     }
> }
> 
> 
> 
>         em.getTransaction().begin();
> 
>         Message m = new Message(60);
>         em.persist(m);
>         System.out.println(m.getModified());
> 
>         em.getTransaction().commit();
> 
> 
> Regards,
> Milosz
> 


Reply via email to