-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with @Generated(GenerationTime.INSERT)
PostPosted: Tue Feb 20, 2007 12:33 am 
Newbie

Joined: Tue Feb 20, 2007 12:25 am
Posts: 1
Location: Salt Lake City, UT
Hi All,

I have an entity "User" that has a timestamp property "createdDate", which I would like to set as the current date when the entity is created, and never change again. I am using Hibernate 3.2.1 GA with a Java 6
compiler on a windows XP machine. Here is the relevant code:

@Entity
public class User
{
...
// An entity must have an identifier
@Id
@GeneratedValue
protected Long id;
...
// Date this user was created
@Temporal(TemporalType.TIMESTAMP)
@Generated(GenerationTime.INSERT)
@Column(insertable = false)
private Date createdDate;

// Last login date
@Temporal(TemporalType.TIMESTAMP)
private Date lastLoginDate;
}

However, when a user entity is saved ...

user = new User(...);
session.save(user);

... the "createdDate" property is null in the database.

My questions are:
- Is that a bug -- should I upgrade to the recent Hibernate 3.2.2 to resolve this issue?
- Or, am I doing something wrong? How to correctly annotate a timestamp
field to be set on creation of an entity?
- How to annotate a property like "lastLoginDate" that should be updated
in some case, but not in others? (not every saveOrUpdate()) Should I be setting this field myself and then call saveOrUpdate()?

Thanks,
Oren

P.S. The SQL generated by Hibernate that is printed to my console is:

17:42:50,062 DEBUG DefaultUserManager:75 - Saving/Updating user SYSTEM
Hibernate: insert into user (email, password, first_name, middle_initial, last_name, gender, age_group, ethnicity, language, last_login_date) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select user_.created_date as created11_19_ from user user_ where user_.id=?

I can't figure out why it generated "select" and not an "insert" in the last statement.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.