-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Event Listeners - Saving Objects
PostPosted: Thu Jul 13, 2006 6:30 am 
Newbie

Joined: Wed Apr 26, 2006 1:10 pm
Posts: 14
Hi all,

I have a object/object (sucessor relation) and i would like to create a new successor automatically when object is updated and update one property of object to fix the new successor.

So when event occured i do :

successor = new Object() ;
// fill properties of successor
object.setSuccessor(successor);

and save these two objects in one transaction.

I try to use Hibernate Event listeners but i can't save the two objects.
I read http://opensource.atlassian.com/project ... e/HHH-1241
and http://forum.hibernate.org/viewtopic.ph ... 84#2275484.

I don't know how to update object and create new one and associate it to the previous with two hibernate sessions.

Someone has any suggestions ?

Hibernate Event Listeners is not very well documented.

Thank you,

Sébastien Boutté


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 9:55 am 
Newbie

Joined: Wed Apr 26, 2006 1:10 pm
Posts: 14
I try to code :

public boolean onPreUpdate(PreUpdateEvent event) {
System.out.println("onPreUpdate");
try {
if (event.getEntity() instanceof PersistentObjectImpl) {
PersistentObjectImpl po = (PersistentObjectImpl) event.getEntity();
if (po.getId() != null) {
ClassMetadata metaData = event.getPersister().getFactory().getClassMetadata(po.getClass());
String[] properties = metaData.getPropertyNames();
Object[] oldValues = event.getOldState();
Object[] newValues = metaData.getPropertyValues(event.getEntity(),EntityMode.POJO);
Session s = event.getPersister().getFactory().openSession(event.getSource().connection());
//Object copy = s.load(po.getClass(),po.getId());
PersistentObjectImpl ancestor = (PersistentObjectImpl) EcaBusinessObject.createBusinessObject(po.getClass(), oldValues, properties);
//ancestor.addSuccesseur((PersistentObjectImpl) copy);
s.save(ancestor);
s.flush();


PersistentObjectImpl copy = (PersistentObjectImpl) event.getSource().internalLoad(ancestor.getClass().getName(),ancestor.getId(),false,false);
//copy.addSuccesseur(po);
po.setPredecesseur((PersistentObjectImpl) copy);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

i i remove the line :
po.setPredecesseur((PersistentObjectImpl) copy);
i can insert the sucessor in the database but not the link with the object.

but if not i have the exception :

GRAVE: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: collection was not processed by flush()
at org.hibernate.engine.CollectionEntry.postFlush(CollectionEntry.java:183)
at org.hibernate.event.def.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:328)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:28)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)


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