HHH-12965 Avoid creating foreign keys between audit and main tables

* Fixes a corner case not addressed by HHH-10667
* Avoids creating foreign-key constraints for any many-to-one
This commit is contained in:
Chris Cranford 2019-09-30 16:31:12 -04:00 committed by Andrea Boriero
parent 02089a5cce
commit b28c7b0761
2 changed files with 43 additions and 3 deletions

View File

@ -311,9 +311,7 @@ public final class IdMetadataGenerator {
// HHH-11107 // HHH-11107
// Use FK hbm magic value 'none' to skip making foreign key constraints between the Envers // 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. // 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() ); MetadataTools.addColumns( manyToOneElement, value.getColumnIterator() );

View File

@ -66,6 +66,48 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper {
data.put( propertyData.getName(), entity ); data.put( propertyData.getName(), entity );
} }
@Override
public void mapToEntityFromEntity(Object objTo, Object objFrom) {
if ( objTo == null || objFrom == null ) {
return;
}
AccessController.doPrivileged(
new PrivilegedAction<Object>() {
@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 @Override
public boolean mapToEntityFromMap(Object obj, Map data) { public boolean mapToEntityFromMap(Object obj, Map data) {
if ( data == null || obj == null ) { if ( data == null || obj == null ) {