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