Hibernate @PrePersist

S

stunaz

Hello,
I want to tinitialize my objetc using the @PrePersist annotations.
Lets say i have an Entiy user:

@Entity
@Table(name = "USERS")
Public Class User {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Integer id;

@Column(name="DATE_UPDATED",nullable = false)
@Temporal(TemporalType.DATE)
@NotNull
private Date dateCreated;

@PrePersist
public void beforeCreate(){
if (this.dateCreated == null) {
this.dateCreated = new Date();
}
}

//getter and setter
}

Somehow, the method beforeCreate is never called when i do :
session.saveOrUpdate(user)
but only when i do session.persist(user)

Can somebody tell me why and how to fix that?
Thanks
 
L

Lew

stunaz said:
I want to tinitialize my objetc using the @PrePersist annotations.
Lets say i [sic] have an Entiy user:

@Entity
@Table(name = "USERS")
Public Class User {

That line won't compile. It's 'public class', not 'Public Class'.
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Integer id;

@Column(name="DATE_UPDATED",nullable = false)
@Temporal(TemporalType.DATE)
@NotNull
private Date dateCreated;

@PrePersist
public void beforeCreate(){
if (this.dateCreated == null) {
this.dateCreated = new Date();
}
}

//getter and setter
}

Somehow, the method beforeCreate is never called when i [sic] do :
session.saveOrUpdate(user)
but only when i [sic] do session.persist(user)

Can somebody tell me why and how to fix that?

Perhaps you should use EntityManager instead of Session.

You should use spaces (maximum four per indent level) rather than TABs in
Usenet posts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top