-->
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: save another entity in PostInsertEventListener
PostPosted: Wed Jun 11, 2008 5:42 pm 
Newbie

Joined: Wed Jun 11, 2008 5:21 pm
Posts: 2
Hi,

im searching for hours but i don't found any breadcrumb to solve my problem. i'm in trouble with a PostInsertEventListener

I have a custom PostInsertEventListener which should create/save another domain object:

Code:
class XY implements PostInsertEventListener {

  onPostInsert( PostInsertEvent event ){
   
    if (event.getEntity() instanceof DomainObject)  {
         EventSource source = event.getSource();
         Session hibernateSession = source.getSession();
         hibernateSession.save( new AnOtherDomainObject( (event.getId() )  );
    }
  }
}


I 'm expecting, that my new AnOtherDomainObject() is 'attached' to my current session and it will by created after Transaction.commit(). But nothing happens.
My DomainObject) is created correctly but it seems tha hibernate ignores my AnOtherDomainObject.

How can I attach/save/create new AnOtherDomainObject in a PostInsertEventListener in my current Session and/or transactional Context? This drives me crazy.

I think i have to use the postinserteventlistener cause i need the generated entity-ID.

Any ideas?

Thanks in advance,
Kuni


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 11:56 am 
Newbie

Joined: Fri Aug 29, 2008 9:07 am
Posts: 16
Location: edinburgh/zielona gora
I have similar problem but I recived org.hibernate.util.JDBCExceptionReporter logExceptions

ventListenerorg.hibernate.exception.GenericJDBCException: could not insert:

Kind of locking problem.

Have you solved your problem by a chance?


cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 12:47 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Quote:
How can I attach/save/create new AnOtherDomainObject in a PostInsertEventListener in my current Session and/or transactional Context? This drives me crazy.

Hi Kuni,

I think you CANNOT add an entity into the current session whilest Hibernate is in the state of persisting the objects of the current session.

I think you have to ressort to another mechanism. You must either open a new session and/or you could have some asynchronous listener which would create the desired entity in another Hibernate session. So your PostInsertEventListener would just create the new object and hand it over to the said listener.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject: new session didnt work
PostPosted: Thu Oct 16, 2008 4:00 am 
Newbie

Joined: Fri Aug 29, 2008 9:07 am
Posts: 16
Location: edinburgh/zielona gora
I tried opening new session but it didnt help. While inserting a new row to database it trigers deadlock error:

Hibernate: insert into simple_log_table (time, id_of_inserteditem) values (?, ?)

Oct 16, 2008 9:51:13 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1205, SQLState: 41000
Oct 16, 2008 9:51:13 AM org.hibernate.util.JDBCExceptionReporter logExceptions
EXCEPTION: PostInsertCustomListener ventListenerorg.hibernate.exception.GenericJDBCException: could not insert: [pl.com.ambsoft.sagro.bo.HistoriaGroby]
SEVERE: Lock wait timeout exceeded; try restarting transaction
Oct 16, 2008 9:51:13 AM org.hibernate.AssertionFailure <init>


Here is body of my onPostInsert listener's body:

if ( event.getEntity() instanceof LogableObject ) {
SessionFactory sessionFactory = Dao.getSessionFactory();
Session s = sessionFactory.openSession();
Transaction tx = s.beginTransaction();

try {

LogableObject curLogableObject = (LogableObject)event.getEntity() ;

System.out.print(curLogableObject.getId()); // it is ok
Date d = Calendar.getInstance().getTime();

LogingEntity hg = new LogingEntity();
hg.setTime(d);
hg.setId_of_inserteditem)(curLogableObject.getId());
s.save(hg);
tx.commit();

} catch (Exception e) {
System.out.println( e);
tx.rollback();
} finally {
s.flush();
s.close();
s = null;
tx=null;
}


thanks for any help


Top
 Profile  
 
 Post subject: Re: save another entity in PostInsertEventListener
PostPosted: Thu Oct 16, 2008 4:27 am 
Newbie

Joined: Fri Aug 29, 2008 9:07 am
Posts: 16
Location: edinburgh/zielona gora
Kuni wrote:
Hi,

im searching for hours but i don't found any breadcrumb to solve my problem. i'm in trouble with a PostInsertEventListener

I have a custom PostInsertEventListener which should create/save another domain object:

Code:
class XY implements PostInsertEventListener {

  onPostInsert( PostInsertEvent event ){
   
    if (event.getEntity() instanceof DomainObject)  {
         EventSource source = event.getSource();
         Session hibernateSession = source.getSession();
         hibernateSession.save( new AnOtherDomainObject( (event.getId() )  );
    }
  }
}


I 'm expecting, that my new AnOtherDomainObject() is 'attached' to my current session and it will by created after Transaction.commit(). But nothing happens.
My DomainObject) is created correctly but it seems tha hibernate ignores my AnOtherDomainObject.

How can I attach/save/create new AnOtherDomainObject in a PostInsertEventListener in my current Session and/or transactional Context? This drives me crazy.

I think i have to use the postinserteventlistener cause i need the generated entity-ID.

Any ideas?

Thanks in advance,
Kuni


Hi Kuni - actually your example helped me solved my problems even it want working in the begining.
Here is an example of your idea working ;) (hibernate version 3.3):

Code:
public class PostInsertCustomListener  implements PostInsertEventListener   {
   
    public void onPostInsert(PostInsertEvent event) {
       

    if ( event.getEntity() instanceof ClassYouNeedsHere ) {
         
          Session s = event.getSession() ;

           try {
             SomeClass sc = new SomeClass();
             sc.setSomething("bla");
             s.save(sc);
              } catch (Exception e) {
                System.out.println(e);
            }

   }
}

}

You are right: no commits, flushes etc. are required.

BTW: A question to hibernate developers team : It seems event.getSession() is depracated - how should I replace it in the future?

Thank you for help.

radek


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.