Hibernate 2.1.x Child Insert Problem - Composite Key On Child
While using hibernate, my parent-child relationship was having (approx.) the following issue.
"Assigned identifiers, including composite identifiers, can't be used to distinguish newly instantiated instances from instances saved or loaded in a previous session. You will need to provide some other way for Hibernate to distinguish."
...which is from this post...
The Interceptor solution is not clearly stated. (the other solutions only seem to work if you have a timestamp/version field in your table, which I don't) The solution is....
Make your model class extend this.
And use this Interceptor when you create your hibernate session.
"Assigned identifiers, including composite identifiers, can't be used to distinguish newly instantiated instances from instances saved or loaded in a previous session. You will need to provide some other way for Hibernate to distinguish."
...which is from this post...
The Interceptor solution is not clearly stated. (the other solutions only seem to work if you have a timestamp/version field in your table, which I don't) The solution is....
Make your model class extend this.
public class Persistent {
private boolean _saved = false;
public void onSave() {
_saved = true;
}
public void onLoad() {
_saved = true;
}
public boolean isSaved() {
return _saved;
}
}
And use this Interceptor when you create your hibernate session.
import java.io.Serializable;
import java.util.Iterator;
import com.td.wss.ft.model.Persistent;
import net.sf.hibernate.CallbackException;
import net.sf.hibernate.Interceptor;
import net.sf.hibernate.type.Type;
public class CompositeKeyFixInterceptor implements Interceptor, Serializable {
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onLoad(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public boolean onLoad(
Object entity,
Serializable id,
Object[] state,
String[] propertyName,
Type[] types) {
if (entity instanceof Persistent) ( (Persistent) entity ).onLoad();
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public boolean onFlushDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyName,
Type[] types)
throws CallbackException {
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public boolean onSave(
Object entity,
Serializable id,
Object[] state,
String[] propertyName,
Type[] types) {
if (entity instanceof Persistent) ( (Persistent) entity ).onSave();
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#onDelete(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public void onDelete(
Object entity,
Serializable id,
Object[] state,
String[] propertyName,
Type[] types)
throws CallbackException {
// do nothing
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#preFlush(java.util.Iterator)
*/
public void preFlush(Iterator entities) throws CallbackException {
// do nothing
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#postFlush(java.util.Iterator)
*/
public void postFlush(Iterator entities) throws CallbackException {
// do nothing
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#isUnsaved(java.lang.Object)
*/
public Boolean isUnsaved(Object entity) {
if (entity instanceof Persistent) {
return new Boolean( !( (Persistent) entity ).isSaved() );
} else {
return null;
}
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#findDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], net.sf.hibernate.type.Type[])
*/
public int[] findDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyName,
Type[] types) {
return null;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Interceptor#instantiate(java.lang.Class, java.io.Serializable)
*/
public Object instantiate(Class clazz, Serializable id)
throws CallbackException {
return null;
}
}


1 Comments:
This comment has been removed by a blog administrator.
Post a Comment
<< Home