public Object replace(
Object original, Object target, SessionImplementor session,
Object owner, Map copyCache, ForeignKeyDirection foreignKeyDirection) throws HibernateException {
boolean include;
if ( isAssociationType() ) {
AssociationType atype = (AssociationType) this;
include = atype.getForeignKeyDirection()==foreignKeyDirection;
}
else {
include = ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT==foreignKeyDirection;
}
return include ? replace(original, target, session, owner, copyCache) : target;
}
Hi,
I was struggling with this issue all past day. Bad news that this is behaviour by design and it is used to merge entities with collections of other entities that use assigned identifiers (IMHO). But it leads to really annoying problems. First is the issue that is mentioned in description and the second is that merging transient entity with collection executes INSERT and subsequent UPDATE of same entity. I use auditing of my entity and don't need this update.
Good news that I've found a simple workaround. It will work only with generated IDs and it is ok for me. I've developed a custom merge listener and registered it:
Of course, it is a hack. Hope core contributors will find a better way to solve these issues.