-->
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.  [ 2 posts ] 
Author Message
 Post subject: HowTo generate a value by the DB like NOW().
PostPosted: Sun Feb 17, 2008 9:13 am 
Newbie

Joined: Sun Feb 17, 2008 9:05 am
Posts: 2
Is it possible to have hibernate generate the value of a column when insterting the row the first time?

I know it is possible for the ids with @Id and @GeneratedValue and @SequenceGenerator yet I was not able to have an entity that has a column which has the default value of NOW().

For example:
Code:
@Entity
@Table(name = "users")
@SequenceGenerator(name = "users_seq", sequenceName = "users_seq")
class User {
  @Id
  @GeneratedValue(generator = "users_seq")
  private long id;
  @Temporal(TemporalType.TIMESTAMP)
  private Date created;
  private String username;
}


Now I would like that created is set to NOW() when the record is inserted. Is that possible? And how?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 17, 2008 9:30 am 
Newbie

Joined: Sun Feb 17, 2008 9:05 am
Posts: 2
If I annotate the field further like so:
Code:
   @Temporal(TemporalType.TIMESTAMP)
   @Column(columnDefinition = "timestamp with time zone not null default now()", insertable = false, updatable = false)
   @GeneratedValue
   private Date registered;


It actually works but this is a postgresql only solution. With MySQL that wouldn't work since the column definition is also not compatible and the NOW() wouldn't be the time of insertion but the time when the definition was made.


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