diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/IdMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/IdMetadataGenerator.java index 1f6d9919f7..0047dd6a69 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/IdMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/IdMetadataGenerator.java @@ -311,9 +311,7 @@ public final class IdMetadataGenerator { // HHH-11107 // Use FK hbm magic value 'none' to skip making foreign key constraints between the Envers // schema and the base table schema when a @ManyToOne is present in an identifier. - if ( mapper == null ) { - manyToOneElement.addAttribute( "foreign-key", "none" ); - } + manyToOneElement.addAttribute( "foreign-key", "none" ); MetadataTools.addColumns( manyToOneElement, value.getColumnIterator() ); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java index 587fac7aa9..26ec6d35db 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java @@ -66,6 +66,48 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper { data.put( propertyData.getName(), entity ); } + @Override + public void mapToEntityFromEntity(Object objTo, Object objFrom) { + if ( objTo == null || objFrom == null ) { + return; + } + + AccessController.doPrivileged( + new PrivilegedAction() { + @Override + public Object run() { + final Getter getter = ReflectionTools.getGetter( + objFrom.getClass(), + propertyData, + getServiceRegistry() + ); + + final Setter setter = ReflectionTools.getSetter( + objTo.getClass(), + propertyData, + getServiceRegistry() + ); + + // Get the value from the containing entity + final Object value = getter.get( objFrom ); + if ( value == null ) { + return null; + } + + if ( !value.getClass().equals( propertyData.getVirtualReturnClass() ) ) { + setter.set( objTo, getAssociatedEntityIdMapper().mapToIdFromEntity( value ), null ); + } + else { + // This means we're setting the object + setter.set( objTo, value, null ); + } + + return null; + } + } + ); + } + @Override public boolean mapToEntityFromMap(Object obj, Map data) { if ( data == null || obj == null ) {