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

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
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: Re: PrePersist, PostLoad, etc not being honoured?
PostPosted: Tue Jun 20, 2006 1:14 pm 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
I'm replying to this post as I've correctly posted it in the EJB3-persistance forum now.

Apologies all!

_________________
--
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.  [ 2 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.