From 494639708288a77bc08209820bc2a9520f5cdd72 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Tue, 5 Dec 2017 10:56:33 +0100 Subject: [PATCH] HHH-12063 - Enable Envers to work with AttributeAccessor annotation. --- .../reader/AuditedPropertiesReader.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java index ec3b89d210..3ddffe0a46 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java @@ -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 propertyAccessedPersistentProperties; + private final Map propertyAccessedPersistentProperties; private final Set fieldAccessedPersistentProperties; // Mapping class field to corresponding element. private final Map 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 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 properties, - String accessType, + Function accessTypeProvider, Set 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