HHH-13945: fallback to call map if exception thrown

This commit is contained in:
Luke Chen 2020-11-04 17:33:03 +08:00 committed by Christian Beikov
parent a975fbf5d4
commit 2228bd238e
1 changed files with 11 additions and 1 deletions

View File

@ -21,6 +21,8 @@ import org.hibernate.envers.internal.tools.query.Parameters;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;
import javax.persistence.PersistenceException;
/**
* @author Adam Warski (adam at warski dot org)
* @author HernпїЅn Chanfreau
@ -58,8 +60,16 @@ public class ToOneIdMapper extends AbstractToOneMapper {
// bi-directional relation, we always store the "old", unchanged data, to prevent storing changes made
// to this field. It is the responsibility of the collection to properly update it if it really changed.
Object entity = nonInsertableFake ? oldObj : newObj;
// fix HHH-13760 - try to aggressively un-proxy this entity to help get the correct type of data later
// in mapToMapFromEntity. But it might fail while getImplementation() if object is deleted or other reasons.
// We catch the exception and fallback to call mapToMapFromEntity directly with the HibernateProxy entity
if ( lazyMapping && entity instanceof HibernateProxy ) {
entity = ( (HibernateProxy) entity ).getHibernateLazyInitializer().getImplementation();
try {
entity = ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
} catch ( PersistenceException e ) {
// Ignore the exception and fallback to call mapToMapFromEntity directly
}
}
delegate.mapToMapFromEntity( newData, entity );