This means option 4 is for you.
Make your own identifier Class and Interceptor. In my cas it's even a component.
My DAO has a createNewInstance Method witch does the id assignement. This way you don't have to worry about this in your business logic.
HTH
Ernst
The identifier class:
Quote:
package hello;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class CapiPK implements Serializable {
private Long mySystemId = null;
private Long myPid = null;
private boolean isUnsaved = true;
public CapiPK() {
super();
}
public CapiPK(Long systemId, Long pid) {
this();
mySystemId = systemId;
myPid = pid;
}
public CapiPK(long systemId, long pid) {
super();
mySystemId = new Long(systemId);
myPid = new Long(pid);
}
public Long getPid() {
return myPid;
}
public void setPid(Long pid) {
myPid = pid;
}
public Long getSystemId() {
return mySystemId;
}
public void setSystemId(Long systemId) {
mySystemId = systemId;
}
public boolean equals(Object obj) {
if (!(obj instanceof CapiPK)) return false;
CapiPK pk = (CapiPK)obj;
return new EqualsBuilder()
.append(mySystemId, pk.mySystemId)
.append(myPid, pk.myPid)
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(mySystemId)
.append(myPid)
.toHashCode();
}
public void setSaved() {
isUnsaved = false;
}
public Boolean isUnsaved() {
return isUnsaved ? Boolean.TRUE : Boolean.FALSE;
}
}
The Interceptor implementation:
Quote:
package hello;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import net.sf.hibernate.CallbackException;
import net.sf.hibernate.Interceptor;
import net.sf.hibernate.type.Type;
public class CapiInterceptor implements Interceptor {
public boolean onLoad(Object arg0, Serializable id, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
if (id instanceof CapiPK) {
((CapiPK)id).setSaved();
}
return false;
}
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] arg5) throws CallbackException {
return false;
}
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) throws CallbackException {
if (id instanceof CapiPK) {
((CapiPK)id).setSaved();
}
return false;
}
public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
}
public void preFlush(Iterator arg0) throws CallbackException {
return;
}
public void postFlush(Iterator arg0) throws CallbackException {
return;
}
public Boolean isUnsaved(Object object) {
try {
Method m = object.getClass().getMethod("getPk", null);
CapiPK pk = (CapiPK) m.invoke(object, null);
return pk.isUnsaved();
} catch (SecurityException e) {
throw new RuntimeException(this.getClass()+" cannot call Class.getMethod, due to missing permissions. " +
"Please make shure the appropriate permissions are granted.", e);
} catch (NoSuchMethodException e) {
// fine, let Hibernat do the rest
return null;
} catch (IllegalArgumentException e) {
// should never happen
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException("The getPk method of the class "+object.getClass()+" isn't public.");
} catch (InvocationTargetException e) {
// should never happen
throw new RuntimeException(e);
}
}
public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) {
return null;
}
public Object instantiate(Class arg0, Serializable arg1)
throws CallbackException {
return null;
}
}