From: stunaz on
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

From: Lew on
stunaz wrote:
> 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.

--
Lew
From: stunaz on
how to use EntityManager instead of Session?
From: Lew on
stunaz wrote:
> how to use EntityManager instead of Session?

<http://download.oracle.com/docs/cd/E17477_01/javaee/5/tutorial/doc/>
specifically
<http://download.oracle.com/docs/cd/E17477_01/javaee/5/tutorial/doc/
bnbqw.html#bnbqy>

<http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/
html/>

--
Lew