-->
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: PrePersist / Annotation Issues
PostPosted: Fri May 01, 2009 6:39 pm 
Newbie

Joined: Fri May 01, 2009 6:21 pm
Posts: 1
Hi,

I'm having trouble getting my PrePersist or PreUpdate annotation to fire.

My config
~~~~~~~~~~~~~~~~~~~~~~~
JDK=1.6.0_13
Hibernate-dist=3.3.1
Hibernate-annotations=3.4
Hibernate-entitymanager=3.4
~~~~~~~~~~~~~~~~~~~~~~~~~

my Hibernate.cfg.xml (no orm.xml specified/used):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">codefu</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/invest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="com.invesys.invest.dao.User" />
<mapping class="com.invesys.invest.dao.EventImpacts" />
</session-factory>
</hibernate-configuration>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

User POJO class with annotations(generated by hibernate tools, then hand editted):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package com.invesys.invest.dao;

// Generated May 1, 2009 12:30:46 AM by Hibernate Tools 3.2.4.CR1





import java.util.Date;

import java.util.HashSet;

import java.util.Set;

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.PrePersist;

import javax.persistence.PreUpdate;



import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Id;

import javax.persistence.OneToMany;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;



/**

* User generated by hbm2java

*/

@Entity

@Table(name="user"

,catalog="invest"

)

public class User implements java.io.Serializable {





private Integer id;

private String name;

private String email;

private String passwordHash;

private Date createdAt;

private Date updatedAt;

private Set<EventImpact> eventImpacts = new HashSet<EventImpact>(0);


public User() {

System.out.println("Created user.");



}



public User(String name, String email, String passwordHash, Date createdAt, Date updatedAt, Set<EventImpact> eventImpacts, Set<InvestGoal> investGoals, Set<UserPortfolio> userPortfolios) {

this.name = name;

this.email = email;

this.passwordHash = passwordHash;

this.createdAt = createdAt;

this.updatedAt = updatedAt;

this.eventImpacts = eventImpacts;

}



@Id @GeneratedValue(strategy=IDENTITY)

@Column(name="id", unique=true, nullable=false)

public Integer getId() {

return this.id;

}



public void setId(Integer id) {

this.id = id;

}




@Column(name="name", length=45)

public String getName() {

return this.name;

}



public void setName(String name) {

this.name = name;

}





@Column(name="email")

public String getEmail() {

return this.email;

}



public void setEmail(String email) {

this.email = email;

}




@Column(name="password_hash", length=128)

public String getPasswordHash() {

return this.passwordHash;

}



public void setPasswordHash(String passwordHash) {

this.passwordHash = passwordHash;

}



@Temporal(TemporalType.TIMESTAMP)
@Column(name="created_at")

public Date getCreatedAt() {

return this.createdAt;

}



@PrePersist

public void onCreate() {

createdAt = new Date();

System.out.println("PrePersist - set createdAt " + createdAt);

}



public void setCreatedAt(Date createdAt) {

this.createdAt = createdAt;

}



@Temporal(TemporalType.TIMESTAMP)

@Column(name="updated_at")

public Date getUpdatedAt() {

return this.updatedAt;

}



public void setUpdatedAt(Date updatedAt) {

this.updatedAt = updatedAt;

}

@PreUpdate

public void onUpdate() {

updatedAt = new Date();

System.out.println("PreUpdate - set updatedAt " + updatedAt);

}



@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="user")

public Set<EventImpact> getEventImpacts() {

return this.eventImpacts;

}



public void setEventImpacts(Set<EventImpact> eventImpacts) {

this.eventImpacts = eventImpacts;

}



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When I create a new User record I don't see my PrePersist get called.
And when I update an existing User record, I don't see my PreUpdate get called.
I don't use the orm.xml (it doesn't appear to be needed?)

For creating a new user I do:
~~~~~~~~~~~~~~~~~~~~~
user = new User() ;
user.setName(name);
user.setEmail(email);
user.setPasswordHash(password);
session.save(user);
session.getTransaction().commit();

But the user.createdAt field is null, and it get's stored in the database as "NULL".

Any ideas?

Thanks much in advance.

sg


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.