-->
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.  [ 7 posts ] 
Author Message
 Post subject: PrePersist, PostLoad, etc not being honoured?
PostPosted: Tue Jun 20, 2006 1:13 pm 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
(cross-posted to Hibernate Users as I didn't know about this forum - sorry all!)

Problem outline

The code below is an attempt to implement Created By/Date and Updated By/Date functionality in our Entity Beans.

The @PrePersist, etc annotations are copied almost exactly from the example from JSR-220 sec 3.5.3.

The issue is that the log statements at the beginning of the Pre/Post methods are never being displayed. I would expect PostLoad and PreUpdate both to be called.

Grepping the debug log output shows no reference to any of these EntityListeners at all.

Thanks in advance!

Christian Ashby
Spiralinks, Inc.

Hibernate version:
3.2 cr2
w/ Annotations 3.2.0.CR1

Mapping documents:
Code:
<hibernate-configuration>
    <session-factory>
        <property name="dialect">@HIBERNATE_DIALECT@</property>
        <property name="show_sql">true</property>
        <property name="use_outer_join">true</property>
        <property name="connection.username">@HIBERNATE_USERNAME@</property>
        <property name="connection.password">@HIBERNATE_PASSWORD@</property>
        <property name="connection.driver_class">@HIBERNATE_DRIVER@</property>
        <property name="connection.url">@HIBERNATE_JDBC_URL@</property>
        <property name="hibernate.c3p0.max_size">200</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">0</property>
        <property name="hibernate.c3p0.max_statements">0</property>
        <property name="hibernate.c3p0.idle_test_period">300</property>
        <property name="hibernate.c3p0.acquire_increment">1</property>
       
        <mapping package="com.spl.focal.beans"/>
        <mapping class="com.spl.focal.beans.FocalEmployee"/>
    </session-factory>
</hibernate-configuration>


Code between sessionFactory.openSession() and session.close():
Code:
Session t = BeanFactory.getSession();
FocalEmployee ee = (FocalEmployee) t.load(FocalEmployee.class, "123");
Transaction tx = t.beginTransaction();
ee.setCorporateTitle("Blah123");
t.save(ee);
tx.commit();
try {
    t.connection().commit();
} catch (HibernateException e) {
    log.error(e);
} catch (SQLException e) {
    log.error(e);
}


Entity bean code
Code:
@Entity
@Table(name="FR_EMPLOYEES")
public class FocalEmployee extends SPLEntityBean {
   private Long personId;
<snip/>
       @Id @Column(name="PERSON_ID")
   @GeneratedValue(generator="PERSON_ID_SEQ")
   public Long getPersonId() {
      return personId;
   }
   public void setPersonId(Long personId) {
      this.personId = personId;
      setIdentifier(personId);
   }
<snip/>
}


Superclass code
Code:
@MappedSuperclass
@EntityListeners(EntityBeanMonitor.class)
public abstract class SPLEntityBean implements IEntityBean {
   private Serializable id = null;

   private Long createdBy;
   private Date createdDate;
   private Long updatedBy;
   private Date updatedDate;
   
   @Transient
   public Serializable getIdentifier() {
      return id;
   }
   public void setIdentifier(Serializable id) {
      this.id = id;
   }
   
   @Column(name="CREATED_BY")
   public Long getCreatedBy() {
      return createdBy;
   }
   public void setCreatedBy(Long createdBy) {
      this.createdBy = createdBy;
   }

   @Temporal(TemporalType.DATE) @Column(name="CREATED_DT")
   public Date getCreatedDate() {
      return createdDate;
   }
   public void setCreatedDate(Date createdDate) {
      this.createdDate = createdDate;
   }
   
   @Column(name="UPDATED_BY")
   public Long getUpdatedBy() {
      return updatedBy;
   }
   public void setUpdatedBy(Long updatedBy) {
      this.updatedBy = updatedBy;
   }
   
   @Temporal(TemporalType.DATE) @Column(name="UPDATED_DT")
   public Date getUpdatedDate() {
      return updatedDate;
   }
   public void setUpdatedDate(Date updatedDate) {
      this.updatedDate = updatedDate;
   }


Monitor class
Code:
public class EntityBeanMonitor {
   // This fairly obscure means of getting the class name
   // will ensure that objects will declare themselves properly
   protected transient static Log log = LogFactory.getLog(new Throwable().getStackTrace()[0].getClassName());
   
   /**
    * This method insures that the CreatedBy / CreatedDate
    * properties are set correctly.
    */
   @PrePersist
   public void onPrePersist(SPLEntityBean bean) {
      log.debug("In onPrePersist()");
      bean.setCreatedBy(new Long(1234));
      bean.setCreatedDate(Calendar.getInstance().getTime());
      bean.setUpdatedBy(new Long(1234));
      bean.setUpdatedDate(Calendar.getInstance().getTime());
   }
   
   /**
    * This method insures that the UpdatedBy / UpdatedDate
    * properties are set correctly.
    */
   @PreUpdate
   public void onPreUpdate(SPLEntityBean bean) {
      log.debug("In onPreUpdate()");
      bean.setUpdatedBy(new Long(12345));
      bean.setUpdatedDate(Calendar.getInstance().getTime());
   }
   
   @PostLoad
   public void onPostLoad(SPLEntityBean bean) {
      log.debug("In onPostLoad() for " + bean.getIdentifier());
   }
}


Full stack trace of any exception that occurs:
none

Name and version of the database you are using:
Oracle 9i

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 3:59 pm 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
Is the @EntityListeners annotation inherited? I am using some listeners successfully (well, mostly) but never annotated a superclass for that.

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 12:12 pm 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
hlfsousa wrote:
Is the @EntityListeners annotation inherited? I am using some listeners successfully (well, mostly) but never annotated a superclass for that.


Thanks for the reply. I've moved the @EntityListeners to the FocalEmployee class, but no cigar - I never get the msg that is logged in the Pre/PostX methods.

Any other ideas? If you know of an example on the web that uses them, that would be a great start...

Thanks again,

Christian

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 3:21 pm 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
Let us get some symptoms:
1. Place a no-argument constructor in EntityBeanMonitor logging that the monitor has been created. This should happen when loading the entities, at application startup.
2. Ease the burden on Hibernate: change the parameter of your callback methods to Object instead of SPLEntityBean. This way you make sure that typing is not being confused when calling the listener.
3. Keep the @EntityListeners annotation in FocalEmployee entity to make sure it is called.
I know no tutorial or complete examples for entity listeners -- I could not find one either.
See if anything changes. If not, that would be akward.

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 27, 2006 12:48 pm 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
hlfsousa wrote:
Let us get some symptoms:
1. Place a no-argument constructor in EntityBeanMonitor logging that the monitor has been created. This should happen when loading the entities, at application startup.


Did this, and the debug message never got run.

hlfsousa wrote:
2. Ease the burden on Hibernate: change the parameter of your callback methods to Object instead of SPLEntityBean. This way you make sure that typing is not being confused when calling the listener.


Also tried to no avail.

hlfsousa wrote:
3. Keep the @EntityListeners annotation in FocalEmployee entity to make sure it is called.
I know no tutorial or complete examples for entity listeners -- I could not find one either.
See if anything changes. If not, that would be akward.


Yeah, nothing here either...

It's been a while since I posted as I've been snowed under, but if you or anyone knows of a potential solution I'd be really greatful!

Thanks for your further help.

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 11:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you must use Hibernate EntityManager to use the @ callback events.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 3:57 am 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
emmanuel wrote:
you must use Hibernate EntityManager to use the @ callback events.


Ah! That'd be what I was missing then. Guess I'll go read the docs on EntityManager then; I was hoping that Annotations alone would work as the annotations configuration seems over complicated for what I need. Ah well - no rest for the wicked.

Thanks for your response though; I was beginning to despair!

_________________
--
Christian Ashby
Spiralinks, Inc.


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

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.