Hello,
I managed to workaround this problem by setting a custom ReplicateEventListener for the operation replicate.
Code:
public class IdPreservingReplicateEventListener extends DefaultReplicateEventListener {
private static final Log log = LogFactory.getLog(IdPreservingReplicateEventListener.class);
protected Serializable performSaveOrReplicate(Object entity, EntityKey key, EntityPersister persister, boolean useIdentityColumn, Object anything, EventSource source) throws HibernateException {
if (key == null) {
Serializable id = persister.getIdentifier(entity, source.getEntityMode());
key = new EntityKey(id, persister, source.getEntityMode());
if (log.isDebugEnabled()) {
log.debug("Replacing null key with entity key (" + key + ") overriding default behavior of DefaultReplicateEventListener");
}
useIdentityColumn = false;
}
return super.performSaveOrReplicate(entity, key, persister, useIdentityColumn, anything, source);
}
}
With the default behavior the key would be null and useIdentityColumn is true. I both generate the key from the copied entity and set useIdentityColumn to false. This does the trick. The ids from the copied entity (which I defined in my mapping document with a native id generator strategy) are preserved and the new entity has the same id in the other datastore.
For replication I use:
Code:
session.replicate(entity, ReplicationMode.OVERWRITE);
The hibernate event system and how to set the custom listener is described in the hibernate reference documentation (version 3.1.3) in chapter 12.2. It can also be set with Spring in the session factory bean.