-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Event listeners
PostPosted: Wed Feb 18, 2009 9:23 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
I am trying to log when an object is commited to the db,

I have this class:
Code:
public class LogListener implements PreInsertEventListener, PreUpdateEventListener
{
  public boolean onPreUpdate(PreUpdateEvent aEvent)
  {
    //code
  }
   public boolean onPreInsert(PreInsertEvent aEvent)
  {
    //code
  }
}



In my web.xml I have the follwing lines of config:

Code:
<hibernate-configuration>
  <session-factory>
    <event type="save">
       <listener class="mycompany.data.LogListener"/>
    </event>
        <event type="update">
       <listener class="mycompany.data.LogListener"/>
    </event>
  </session-factory>
</hibernate-configuration>


Now when I execute a saveOrUpdate method in one of my dao's the onPreUpdate / onPreInsert methods are not being called. Am I missing something?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:36 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Try to use event type names like "pre-update" or "pre-insert"

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:44 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
Thanks for the suggestion, I tried it but it is still not going in the method. It is now

Code:
<hibernate-configuration>
  <session-factory>
    <event type="pre-insert">
       <listener class="uk.co.data.AuditLogListener"/>
    </event>
        <event type="pre-update">
       <listener class="uk.co.data.AuditLogListener"/>
    </event>
  </session-factory>
</hibernate-configuration>


Last edited by mcbi4kh2 on Tue May 05, 2009 4:42 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:50 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Are you also using a persistence.xml? In that case you have either to include the listeners or your hibernate.cfg.xml.

Why do you configure hibernate in web.xml?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:55 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
No I'm not using a presistence.xml, should I be?

I point my web.xml to read the myProject-db.xml file which has all my dataSource, sessionFactory, hibernateTemplate beans in.

I tried putting my listeners in there but I get an error saying "Element type 'listener' must be declared".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:00 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
hmm, i have no clue about your project structure. Can you post your structure or your myProject-db.xml? Where did you get that structure from? Any tutorial where you copied it from?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:07 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
Ok Im using Spring MVC and Hibernate to do a web app.

I have my web.xml which has:
Code:
  <!--  Spring Context Loaders -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>



  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/myProject-security.xml,
      classpath:/config/myProject-dao.xml,
      classpath:/config/myProject-db.xml,
      classpath:/config/myProject-services.xml,

    </param-value>
  </context-param>



In myProject-dao.xml I just hava
Code:
<beans>

  <bean id="dao" abstract="true">
    <property name="hibernateTemplate">
      <ref bean="hibernateTemplate" />
    </property>
  </bean>
  <bean id="myDao" parent="dao" class="myCompany.etc"/>


In myProject-db.xml I have the hibernate config stuff
Code:
  <!-- Properties Configuration -->
  <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>
          classpath:config/myProject-hibernate.properties
        </value>
      </list>
    </property>
  </bean>

  <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value="${myProject.connection.url}" />
    <property name="driverClassName"
      value="${myProject.connection.driver_class}" />
     etc
   </bean>


I'm unsure where I need to put my listeners?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:09 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
oops


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:16 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
check to be sure you are registering them correctly.

Code:
EventListeners myListeners = ((SessionFactoryImpl) sessionFactory).getEventListeners();


see the available methods on here to check the various event listener types that are implemented.

http://www.hibernate.org/hib_docs/v3/ap ... eners.html


as for the various EventListener name types, they are available in the EventListeners.java file which goes like:

Code:
      eventInterfaceFromType.put("auto-flush", AutoFlushEventListener.class);
      eventInterfaceFromType.put("merge", MergeEventListener.class);
      eventInterfaceFromType.put("create", PersistEventListener.class);
      eventInterfaceFromType.put("create-onflush", PersistEventListener.class);
      eventInterfaceFromType.put("delete", DeleteEventListener.class);
      eventInterfaceFromType.put("dirty-check", DirtyCheckEventListener.class);
      eventInterfaceFromType.put("evict", EvictEventListener.class);
      eventInterfaceFromType.put("flush", FlushEventListener.class);
      eventInterfaceFromType.put("flush-entity", FlushEntityEventListener.class);
      eventInterfaceFromType.put("load", LoadEventListener.class);
      eventInterfaceFromType.put("load-collection", InitializeCollectionEventListener.class);
      eventInterfaceFromType.put("lock", LockEventListener.class);
      eventInterfaceFromType.put("refresh", RefreshEventListener.class);
      eventInterfaceFromType.put("replicate", ReplicateEventListener.class);
      eventInterfaceFromType.put("save-update", SaveOrUpdateEventListener.class);
      eventInterfaceFromType.put("save", SaveOrUpdateEventListener.class);
      eventInterfaceFromType.put("update", SaveOrUpdateEventListener.class);
      eventInterfaceFromType.put("pre-load", PreLoadEventListener.class);
      eventInterfaceFromType.put("pre-update", PreUpdateEventListener.class);
      eventInterfaceFromType.put("pre-delete", PreDeleteEventListener.class);
      eventInterfaceFromType.put("pre-insert", PreInsertEventListener.class);
      eventInterfaceFromType.put("pre-collection-recreate", PreCollectionRecreateEventListener.class);
      eventInterfaceFromType.put("pre-collection-remove", PreCollectionRemoveEventListener.class);
      eventInterfaceFromType.put("pre-collection-update", PreCollectionUpdateEventListener.class);
      eventInterfaceFromType.put("post-load", PostLoadEventListener.class);
      eventInterfaceFromType.put("post-update", PostUpdateEventListener.class);
      eventInterfaceFromType.put("post-delete", PostDeleteEventListener.class);
      eventInterfaceFromType.put("post-insert", PostInsertEventListener.class);
      eventInterfaceFromType.put("post-commit-update", PostUpdateEventListener.class);
      eventInterfaceFromType.put("post-commit-delete", PostDeleteEventListener.class);
      eventInterfaceFromType.put("post-commit-insert", PostInsertEventListener.class);
      eventInterfaceFromType.put("post-collection-recreate", PostCollectionRecreateEventListener.class);
      eventInterfaceFromType.put("post-collection-remove", PostCollectionRemoveEventListener.class);
      eventInterfaceFromType.put("post-collection-update", PostCollectionUpdateEventListener.class);


I think you'd get an error if you used the wrong interface on the wrong event type though and it looks like you have them correct.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:48 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
I changed my config to:
Code:
<hibernate-configuration>
  <session-factory>
    <event type="save-update">
       <listener class="mycompany.data.LogListener"/>
    </event>
  </session-factory>
</hibernate-configuration>


But when I break into:
Code:
        EventListeners myListeners = ((SessionFactoryImpl) sessionFactory).getEventListeners();


The value of saveOrUpdateEventListeners in myListeners is "DefaultSaveOrUpdateEventListener".

I presume that value should be "mycompany.data.LogListener"?

Thanks for the help so far


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 11:05 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
LogListener, if its how it was in the first post, isn't really correct. You'd need to extend the DefaultSaveOrUpdateEventListener, and make sure to call super.onSaveOrUpdate(SaveOrUpdateEvent event) at the end of your implementation otherwise an object would never be actually make it to the saveOrUpdate call.

Code:
MyNewSaveUpdateListener extends DefaultSaveOrUpdateEventListener {

public void onSaveOrUpdate() {

blablabla
blalbalbal
super.onSaveOrUpdate(SaveOrUpdateEvent event);
}
}



and make sure you set it as the "save-update" listener

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 11:18 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
yeah that makes sense,

I now have:

Code:
<hibernate-configuration>
  <session-factory>
    <event type="save-update">
       <listener class="myProject.data.LogListener"/>
    </event>
  </session-factory>
</hibernate-configuration>


And my class:

Code:
public class LogListener extends DefaultSaveOrUpdateEventListener

{
  public void onSaveOrUpdate(SaveOrUpdateEvent aEvent) throws HibernateException
  {
    // My code
  }
}


But the saveOrUpdateEventListener is still "DefaultSaveOrUpdateEventListener".

Thanks for the help so far


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 11:33 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I don't know how it's not working. You have it in the correct SessionFactory instance? Checking the type correctly? Making sure you're using the one from the context file? I'm just shooting in the dark, it should work how you have it

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 11:41 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
In spring you have to register your event listeners in a different way. Here is an example I found:
Code:
<beans>
  <bean id="loadListener" class="org.nautsch.MyLoadEventListener" />

  <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
  </bean>

  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
      <list>
        <value>product.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
      </value>
    </property>
    <property name="eventListeners">
        <map>
            <entry key="load"><ref local="loadListener" /></entry>
        </map>
    </property>
  </bean>

</beans>


Also look here.

Rating appreciated. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 11:50 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
that's only if you're using the Spring implementation of the SessionFactory. From the looks of it, he is using the Hibernate one in the hibernate.cfg.xml.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.