Hi,
I'm using Hibernate 2.1.1, Windows 2000, Jdk 1.3 and DB2 7.2. I'm having trouble with a parent-child relationship, as explained in this thread (
http://forum.hibernate.org/viewtopic.php?t=927180).
I'm trying to implement my Involved object as a Persistent class - following the Hibernate documentation chapter 9.4 (
http://www.hibernate.org/hib_docs/reference/html/parent-child.html#parent-child-update). I've also implemented an Interceptor as shown in the same chapter:
Code:
public class MyInterceptor implements Interceptor{
public boolean onLoad(
Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types)
throws CallbackException
{
if (entity instanceof Persistent) {
((Persistent) entity).onLoad();
}
return false;
}
public boolean onSave(
Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types)
throws CallbackException
{
if (entity instanceof Persistent) {
((Persistent) entity).onSave();
}
return false;
}
public Boolean isUnsaved(Object entity) {
if (entity instanceof Persistent) {
return new Boolean(!((Persistent) entity).isSaved());
} else {
return null;
}
}
public boolean onFlushDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types)
throws CallbackException
{
return false;
}
public void onDelete(
Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types)
throws CallbackException
{
}
public void preFlush(Iterator entities) throws CallbackException {
}
public void postFlush(Iterator entities) throws CallbackException {
}
public int[] findDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types)
{
return new int[0];
}
public Object instantiate(Class clazz, Serializable id) throws CallbackException {
return null;
}
}
Then I try this:
Code:
session = SessionFactoryPlant.getSessionFactory().openSession( new MyInterceptor());
tx = session.beginTransaction();
if( case.getId() != null ){ // this is an update!
persistedCase = (Case) session.get( Case.class, case.getId() );
copyForPersistence(persistedCase, case);
session.flush();
} else {
copyForPersistence(persistedCase, case);
session.save( persistedCase );
}
tx.commit();
session.close;
Only the Involved class extends the Persistent class. When I run the code, the saving works fine (new instance). The correct insert statement is invoked and I'm a happy fellow. When I try to update an existing case, no update statement is invoked.
The copyForPersistence method copies all attributes from the case object (which I got from the web layer) to the persistedCase object (which in this case is retrieved from the database).
Why isn't an update invoked when I change a persisted object and invoke commit()?
Thanks for all help!
Regards,
Ola