HHH-12063 - Enable Envers to work with AttributeAccessor annotation.

This commit is contained in:
Chris Cranford 2017-12-05 10:56:33 +01:00
parent d7da90f9cc
commit 4946397082
1 changed files with 16 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import javax.persistence.ElementCollection;
import javax.persistence.JoinColumn;
@ -75,7 +76,7 @@ public class AuditedPropertiesReader {
private final ReflectionManager reflectionManager;
private final String propertyNamePrefix;
private final Set<String> propertyAccessedPersistentProperties;
private final Map<String, String> propertyAccessedPersistentProperties;
private final Set<String> fieldAccessedPersistentProperties;
// Mapping class field to corresponding <properties> element.
private final Map<String, String> propertiesGroupMapping;
@ -100,7 +101,7 @@ public class AuditedPropertiesReader {
this.reflectionManager = reflectionManager;
this.propertyNamePrefix = propertyNamePrefix;
propertyAccessedPersistentProperties = newHashSet();
propertyAccessedPersistentProperties = newHashMap();
fieldAccessedPersistentProperties = newHashSet();
propertiesGroupMapping = newHashMap();
@ -268,7 +269,7 @@ public class AuditedPropertiesReader {
fieldAccessedPersistentProperties.add( property.getName() );
}
else {
propertyAccessedPersistentProperties.add( property.getName() );
propertyAccessedPersistentProperties.put( property.getName(), property.getPropertyAccessorName() );
}
}
@ -314,11 +315,14 @@ public class AuditedPropertiesReader {
Audited audited = computeAuditConfiguration( dynamicComponentSource.getXClass() );
if ( !fieldAccessedPersistentProperties.isEmpty() ) {
throw new MappingException(
"Audited dynamic component cannot have properties with access=\"field\" for properties: " + fieldAccessedPersistentProperties + ". \n Change properties access=\"property\", to make it work)"
"Audited dynamic component cannot have properties with access=\"field\" for properties: " +
fieldAccessedPersistentProperties +
". \n Change properties access=\"property\", to make it work)"
);
}
for ( String property : propertyAccessedPersistentProperties ) {
String accessType = AccessType.PROPERTY.getType();
for ( Map.Entry<String, String> entry : propertyAccessedPersistentProperties.entrySet() ) {
String property = entry.getKey();
String accessType = entry.getValue();
if ( !auditedPropertiesHolder.contains( property ) ) {
final Value propertyValue = persistentPropertiesSource.getProperty( property ).getValue();
if ( propertyValue instanceof Component ) {
@ -351,14 +355,14 @@ public class AuditedPropertiesReader {
//look in the class
addFromProperties(
clazz.getDeclaredProperties( "field" ),
"field",
it -> "field",
fieldAccessedPersistentProperties,
allClassAudited
);
addFromProperties(
clazz.getDeclaredProperties( "property" ),
"property",
propertyAccessedPersistentProperties,
propertyAccessedPersistentProperties::get,
propertyAccessedPersistentProperties.keySet(),
allClassAudited
);
@ -372,10 +376,12 @@ public class AuditedPropertiesReader {
private void addFromProperties(
Iterable<XProperty> properties,
String accessType,
Function<String, String> accessTypeProvider,
Set<String> persistentProperties,
Audited allClassAudited) {
for ( XProperty property : properties ) {
final String accessType = accessTypeProvider.apply( property.getName() );
// If this is not a persistent property, with the same access type as currently checked,
// it's not audited as well.
// If the property was already defined by the subclass, is ignored by superclasses