-->
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.  [ 5 posts ] 
Author Message
 Post subject: Post-Update Listener: not saving new Objects ?
PostPosted: Wed Dec 20, 2006 4:04 am 
Beginner
Beginner

Joined: Mon Sep 12, 2005 3:27 am
Posts: 48
Hello,

i'm using an post-update listener with hibernate 3.2.1 GA inside jboss. Have configured correctly persistence.xml and session binding to jndi.

Inside this listener i create a new audit-entry. Then i try to persist this transient object via following code:

Code:
  private void saveNewObjectFromWithinEventListener( Object myObject ) {
        try {
            InitialContext ctx = new InitialContext();
            SessionFactory factory = (SessionFactory) ctx.lookup("java:/hibernate/SessionFactory");
            factory.getCurrentSession().save(myObject);
        } catch (NamingException e) {
            throw new HibernateException(e);
        }


But the Audit entry get's never persisted in the database. No Exception is thrown. Is it impossible to create and persist objects inside an listener ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 7:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you cannot use a session inside it's own event listener

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 4:09 am 
Beginner
Beginner

Joined: Mon Sep 12, 2005 3:27 am
Posts: 48
But is it possible to use another (new) Session inside the listener? For example via:

Code:
factory.openSession(factory.getCurrentSession().connection())
               .persist(myObject);      


Will this work ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 12:18 pm 
Beginner
Beginner

Joined: Mon Sep 12, 2005 3:27 am
Posts: 48
Addition: it seems that even the way via

Code:
factory.getCurrentSession().save(myObject);

is working. I "only" explicitly have to flush() the session.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 4:01 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
a new session on the connection is the safe way to do it

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.