-->
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.  [ 4 posts ] 
Author Message
 Post subject: Sessions in EventListeners
PostPosted: Fri Feb 20, 2009 4:34 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
I am using Hibernate events to try and write to a db each time a saveOrUpdate method is called. It is stepping into this event ok, however the object is not being saved to the db.



Code:
public class MyListener extends DefaultSaveOrUpdateEventListener
{

  public void onSaveOrUpdate(SaveOrUpdateEvent aEvent) throws HibernateException
  {
   
    //code
    Session session = aEvent.getSession().getSessionFactory().openSession();
    session.save(myObject);
   


My hibernate config stuff.

Code:
   <bean id="saveUpdateListener"
         class="myCompany.data.MyListener" />


  <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="eventListeners">
      <map>
        <entry key="save-update">
          <ref local="saveUpdateListener" />
        </entry>
      </map>
    </property>


I have also tried in MyListener to get the session via:

Code:
Session session = aEvent.getSession();


That doesnt work either, any ideas why?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 6:58 am 
Beginner
Beginner

Joined: Thu Feb 19, 2009 5:48 am
Posts: 37
Location: Glasgow, Scotland
Hey, I'm just new myself but don't you need to begin and commit a transaction?


Code:
Session session = aEvent.getSession().getSessionFactory().openSession();
session.beginTransaction();
session.save(myObject);
session.getTransaction().commit();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 7:05 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
Quite right, you do. Good work. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 7:17 am 
Beginner
Beginner

Joined: Thu Feb 19, 2009 5:48 am
Posts: 37
Location: Glasgow, Scotland
No problem, how about rating my post so that I have credits to ask some of my stupid questions later? :P


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.