Skip to content

Commit

Permalink
HHH-11895 Support traversal of components in audit query API
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Feisst authored and Naros committed Dec 17, 2021
1 parent 2542173 commit d9f3e82
Show file tree
Hide file tree
Showing 42 changed files with 1,121 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.envers.boot.spi.EnversMetadataBuildingContext;
import org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData;
import org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData;
import org.hibernate.envers.internal.entities.EntityConfiguration;
import org.hibernate.envers.internal.entities.mapper.CompositeMapperBuilder;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Property;
Expand Down Expand Up @@ -80,6 +81,11 @@ public void addComponent(
);
}
}

if ( !firstPass ) {
final EntityConfiguration owningEntityConfiguration = getAuditedEntityConfigurations().get( entityName );
owningEntityConfiguration.addToOneComponent( propertyAuditingData.getName(), componentAuditingData );
}
}

private String getClassNameForComponent(Component component) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private void storeMiddleEntityRelationInformation(
MiddleIdData referencingIdData,
String referencedPrefix,
String auditMiddleEntityName) {
// Only if this is a relation (when there is a referenced entity).
// Only if this is a relation (when there is a referenced entity or a component).
if ( context.getReferencedEntityName() != null ) {
final IdMappingData referencedIdMapping = getReferencedIdMappingData(
context.getReferencingEntityName(),
Expand Down Expand Up @@ -270,5 +270,12 @@ private void storeMiddleEntityRelationInformation(
);
}
}
else {
context.getReferencingEntityConfiguration().addToManyComponent(
context.getPropertyName(),
auditMiddleEntityName,
referencingIdData
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.internal.entities;

import org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData;
import org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData;

/**
* @author Felix Feisst (feisst dot felix at gmail dot com)
*/
public class ComponentDescription {

private final ComponentType type;
private final String propertyName;
private final String auditMiddleEntityName;
private final MiddleIdData middleIdData;
private final ComponentAuditingData auditingData;

private ComponentDescription(ComponentType type,
String propertyName,
String auditMiddleEntityName,
MiddleIdData middleIdData,
ComponentAuditingData componentAuditingData) {
this.type = type;
this.propertyName = propertyName;
this.auditMiddleEntityName = auditMiddleEntityName;
this.middleIdData = middleIdData;
this.auditingData = componentAuditingData;
}

public static ComponentDescription many(String propertyName, String auditMiddleEntityName, MiddleIdData middleIdData) {
return new ComponentDescription( ComponentType.MANY, propertyName, auditMiddleEntityName, middleIdData, null );
}

public static ComponentDescription one(String propertyName, ComponentAuditingData componentAuditingData) {
return new ComponentDescription( ComponentType.ONE, propertyName, null, null, componentAuditingData );
}

public ComponentType getType() {
return type;
}

public String getPropertyName() {
return propertyName;
}

public String getAuditMiddleEntityName() {
return auditMiddleEntityName;
}

public MiddleIdData getMiddleIdData() {
return middleIdData;
}

public ComponentAuditingData getAuditingData() {
return auditingData;
}

public enum ComponentType {
ONE, MANY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ private Collection<RelationDescription> getRelationDescriptions(String entityNam
return descriptions;
}

public ComponentDescription getComponentDescription(final String entityName, final String propertyName) {
final EntityConfiguration entCfg;
if ( isVersioned( entityName ) ) {
entCfg = get( entityName );
}
else {
entCfg = getNotVersionEntityConfiguration( entityName );
}
final ComponentDescription relDesc = entCfg.getComponentDescription( propertyName );
if ( relDesc != null ) {
return relDesc;
}
else if ( entCfg.getParentEntityName() != null ) {
// The field may be declared in a superclass ...
return getComponentDescription( entCfg.getParentEntityName(), propertyName );
}
else {
return null;
}
}

private void addWithParentEntityNames(String entityName, Set<String> entityNames) {
entityNames.add( entityName );
final EntityConfiguration entCfg = entitiesConfigurations.get( entityName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.Map;

import org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData;
import org.hibernate.envers.internal.entities.mapper.ExtendedPropertyMapper;
import org.hibernate.envers.internal.entities.mapper.PropertyMapper;
import org.hibernate.envers.internal.entities.mapper.id.IdMapper;
Expand All @@ -31,6 +32,7 @@ public class EntityConfiguration {
private final ExtendedPropertyMapper propertyMapper;
// Maps from property name
private final Map<String, RelationDescription> relations;
private final Map<String, ComponentDescription> components;
private final String parentEntityName;

public EntityConfiguration(
Expand All @@ -46,6 +48,7 @@ public EntityConfiguration(
this.parentEntityName = parentEntityName;

this.relations = new HashMap<>();
this.components = new HashMap<>();
}

public void addToOneRelation(
Expand Down Expand Up @@ -170,6 +173,14 @@ public void addToManyMiddleNotOwningRelation(
);
}

public void addToManyComponent(String propertyName, String auditMiddleEntityName, MiddleIdData middleIdData) {
components.put( propertyName, ComponentDescription.many( propertyName, auditMiddleEntityName, middleIdData ) );
}

public void addToOneComponent(String propertyName, ComponentAuditingData auditingData) {
components.put( propertyName, ComponentDescription.one( propertyName, auditingData ) );
}

public boolean isRelation(String propertyName) {
return relations.get( propertyName ) != null;
}
Expand All @@ -178,6 +189,10 @@ public RelationDescription getRelationDescription(String propertyName) {
return relations.get( propertyName );
}

public ComponentDescription getComponentDescription(String propertyName) {
return components.get( propertyName );
}

public IdMappingData getIdMappingData() {
return idMappingData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,15 @@ public void addWhereOrNullRestriction(String left, boolean addAliasLeft, String
*
* @param configuration the configuration.
* @param aliasToEntityNameMap alias to entity name map, never {@literal null}
* @param aliasToComponentPropertyNameMap alias to component property name map, never {@literal null}
* @param function the function.
* @param op the operator.
* @param value the scalar value.
*/
public void addWhereWithFunction(
Configuration configuration,
Map<String, String> aliasToEntityNameMap,
Map<String, String> aliasToComponentPropertyNameMap,
AuditFunction function,
String op,
Object value) {
Expand All @@ -333,6 +335,7 @@ public void addWhereWithFunction(
QueryBuilder.appendFunctionArgument(
configuration,
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
queryParamCounter,
localQueryParamValues,
alias,
Expand All @@ -354,6 +357,7 @@ public void addWhereWithFunction(
*
* @param configuration the configuration.
* @param aliasToEntityNameMap alias to entity name map, never {@literal null}
* @param aliasToComponentPropertyNameMap alias to component property name map, never {@literal null}
* @param function the function.
* @param op the operator.
* @param aliasRight the optional alias of the right property, may be {@literal null}
Expand All @@ -362,6 +366,7 @@ public void addWhereWithFunction(
public void addWhereWithFunction(
Configuration configuration,
Map<String, String> aliasToEntityNameMap,
Map<String, String> aliasToComponentPropertyNameMap,
AuditFunction function,
String op,
String aliasRight,
Expand All @@ -371,6 +376,7 @@ public void addWhereWithFunction(
QueryBuilder.appendFunctionArgument(
configuration,
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
queryParamCounter,
localQueryParamValues,
alias,
Expand All @@ -393,6 +399,7 @@ public void addWhereWithFunction(
*
* @param configuration the configuration.
* @param aliasToEntityNameMap alias to entity name map, never {@literal null}
* @param aliasToComponentPropertyNameMap alias to component property name map, never {@literal null}
* @param aliasLeft the optional alias of the left property, may be {@literal null}
* @param left the property.
* @param op the operator.
Expand All @@ -401,6 +408,7 @@ public void addWhereWithFunction(
public void addWhereWithFunction(
Configuration configuration,
Map<String, String> aliasToEntityNameMap,
Map<String, String> aliasToComponentPropertyNameMap,
String aliasLeft,
String left,
String op,
Expand All @@ -417,6 +425,7 @@ public void addWhereWithFunction(
QueryBuilder.appendFunctionArgument(
configuration,
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
queryParamCounter,
localQueryParamValues,
alias,
Expand All @@ -432,13 +441,15 @@ public void addWhereWithFunction(
*
* @param configuration the configuration.
* @param aliasToEntityNameMap alias to entity name map, never {@literal null}
* @param aliasToComponentPropertyNameMap alias to component property name map, never {@literal null}
* @param left the left-side function.
* @param op the operator.
* @param right the right-side function.
*/
public void addWhereWithFunction(
Configuration configuration,
Map<String, String> aliasToEntityNameMap,
Map<String, String> aliasToComponentPropertyNameMap,
AuditFunction left,
String op,
AuditFunction right) {
Expand All @@ -447,6 +458,7 @@ public void addWhereWithFunction(
QueryBuilder.appendFunctionArgument(
configuration,
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
queryParamCounter,
localQueryParamValues,
alias,
Expand All @@ -459,6 +471,7 @@ public void addWhereWithFunction(
QueryBuilder.appendFunctionArgument(
configuration,
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
queryParamCounter,
localQueryParamValues,
alias,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.hibernate.envers.internal.entities.mapper.id.QueryParameterData;
import org.hibernate.envers.internal.tools.MutableInteger;
import org.hibernate.envers.internal.tools.StringTools;
import org.hibernate.envers.internal.tools.Triple;
import org.hibernate.envers.query.criteria.AuditFunction;
import org.hibernate.envers.query.criteria.AuditId;
import org.hibernate.envers.query.criteria.AuditProperty;
import org.hibernate.envers.query.criteria.internal.CriteriaTools;
import org.hibernate.envers.query.order.NullPrecedence;
import org.hibernate.envers.tools.Pair;
import org.hibernate.query.Query;
Expand Down Expand Up @@ -220,11 +220,16 @@ public void addProjection(String function, String alias, String propertyName, bo
}
}

public void addProjection(Configuration configuration, Map<String, String> aliasToEntityNameMap, AuditFunction function) {
public void addProjection(
Configuration configuration,
Map<String, String> aliasToEntityNameMap,
Map<String, String> aliasToComponentPropertyNameMap,
AuditFunction function) {
final StringBuilder expression = new StringBuilder();
appendFunctionArgument(
configuration,
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
paramCounter,
projectionQueryParamValues,
alias,
Expand All @@ -237,6 +242,7 @@ public void addProjection(Configuration configuration, Map<String, String> alias
protected static void appendFunctionArgument(
Configuration configuration,
Map<String, String> aliasToEntityNameMap,
Map<String, String> aliasToComponentPropertyNameMap,
MutableInteger paramCounter,
Map<String, Object> queryParamValues,
String alias,
Expand All @@ -253,6 +259,7 @@ protected static void appendFunctionArgument(
appendFunctionArgument(
configuration,
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
paramCounter,
queryParamValues,
alias,
Expand Down Expand Up @@ -292,8 +299,14 @@ else if ( argument instanceof AuditProperty ) {
if ( propertyAlias != null ) {
expression.append( propertyAlias ).append( '.' );
}
String propertyPrefix = CriteriaTools.determineComponentPropertyPrefix(
configuration.getEnversService(),
aliasToEntityNameMap,
aliasToComponentPropertyNameMap,
propertyAlias
);
String propertyName = property.getPropertyNameGetter().get( configuration );
expression.append( propertyName );
expression.append( propertyPrefix.concat( propertyName ) );
}
else {
String queryParam = "_p" + paramCounter.getAndIncrease();
Expand Down

0 comments on commit d9f3e82

Please sign in to comment.