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.
|