Search in sources :

Example 1 with AuditEntitiesConfiguration

use of org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration in project hibernate-orm by hibernate.

the class AbstractAuditWorkUnit method fillDataWithId.

protected void fillDataWithId(Map<String, Object> data, Object revision) {
    final AuditEntitiesConfiguration entitiesCfg = enversService.getAuditEntitiesConfiguration();
    final Map<String, Object> originalId = new HashMap<>();
    originalId.put(entitiesCfg.getRevisionFieldName(), revision);
    enversService.getEntitiesConfigurations().get(getEntityName()).getIdMapper().mapToMapFromId(originalId, id);
    data.put(entitiesCfg.getRevisionTypePropName(), revisionType);
    data.put(entitiesCfg.getOriginalIdPropName(), originalId);
}
Also used : HashMap(java.util.HashMap) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration)

Example 2 with AuditEntitiesConfiguration

use of org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration in project hibernate-orm by hibernate.

the class EntitiesModifiedAtRevisionQuery method list.

@Override
public List list() {
    /*
         * The query that we need to create:
         *   SELECT new list(e) FROM versionsReferencedEntity e
         *   WHERE
         * (all specified conditions, transformed, on the "e" entity) AND
         * e.revision = :revision
         */
    AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration();
    String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
    qb.getRootParameters().addWhereWithParam(revisionPropertyPath, "=", revision);
    // all specified conditions
    for (AuditCriterion criterion : criterions) {
        criterion.addToQuery(enversService, versionsReader, aliasToEntityNameMap, QueryConstants.REFERENCED_ENTITY_ALIAS, qb, qb.getRootParameters());
    }
    for (final AuditAssociationQueryImpl<?> associationQuery : associationQueries) {
        associationQuery.addCriterionsToQuery(versionsReader);
    }
    Query query = buildQuery();
    List queryResult = query.list();
    return applyProjections(queryResult, revision);
}
Also used : Query(org.hibernate.query.Query) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion) List(java.util.List)

Example 3 with AuditEntitiesConfiguration

use of org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration in project hibernate-orm by hibernate.

the class EnversServiceImpl method doInitialize.

private void doInitialize(final MetadataImplementor metadata, final MappingCollector mappingCollector, ServiceRegistry serviceRegistry) {
    final ConfigurationService cfgService = serviceRegistry.getService(ConfigurationService.class);
    final Properties properties = new Properties();
    properties.putAll(cfgService.getSettings());
    this.globalConfiguration = new GlobalConfiguration(this, properties);
    final ReflectionManager reflectionManager = metadata.getMetadataBuildingOptions().getReflectionManager();
    final RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration(globalConfiguration);
    final RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(metadata, reflectionManager);
    EnversServiceImpl.this.auditEntitiesConfiguration = new AuditEntitiesConfiguration(properties, revInfoCfgResult.getRevisionInfoEntityName(), this);
    this.auditProcessManager = new AuditProcessManager(revInfoCfgResult.getRevisionInfoGenerator());
    this.revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator();
    this.revisionInfoNumberReader = revInfoCfgResult.getRevisionInfoNumberReader();
    this.modifiedEntityNamesReader = revInfoCfgResult.getModifiedEntityNamesReader();
    this.auditStrategy = initializeAuditStrategy(auditEntitiesConfiguration.getAuditStrategyName(), revInfoCfgResult.getRevisionInfoClass(), revInfoCfgResult.getRevisionInfoTimestampData(), serviceRegistry);
    this.entitiesConfigurations = new EntitiesConfigurator().configure(metadata, serviceRegistry, reflectionManager, mappingCollector, globalConfiguration, auditEntitiesConfiguration, auditStrategy, revInfoCfgResult.getRevisionInfoXmlMapping(), revInfoCfgResult.getRevisionInfoRelationMapping());
}
Also used : GlobalConfiguration(org.hibernate.envers.configuration.internal.GlobalConfiguration) AuditProcessManager(org.hibernate.envers.internal.synchronization.AuditProcessManager) EntitiesConfigurator(org.hibernate.envers.configuration.internal.EntitiesConfigurator) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) RevisionInfoConfiguration(org.hibernate.envers.configuration.internal.RevisionInfoConfiguration) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) RevisionInfoConfigurationResult(org.hibernate.envers.configuration.internal.RevisionInfoConfigurationResult) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Properties(java.util.Properties)

Example 4 with AuditEntitiesConfiguration

use of org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration in project hibernate-orm by hibernate.

the class AuditAssociationQueryImpl method addCriterionsToQuery.

protected void addCriterionsToQuery(AuditReaderImplementor versionsReader) {
    if (enversService.getEntitiesConfigurations().isVersioned(entityName)) {
        String auditEntityName = enversService.getAuditEntitiesConfiguration().getAuditEntityName(entityName);
        Parameters joinConditionParameters = queryBuilder.addJoin(joinType, auditEntityName, alias, false);
        // owner.reference_id = target.originalId.id
        AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration();
        String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
        IdMapper idMapperTarget = enversService.getEntitiesConfigurations().get(entityName).getIdMapper();
        final String prefix = alias.concat(".").concat(originalIdPropertyName);
        ownerAssociationIdMapper.addIdsEqualToQuery(joinConditionParameters, ownerAlias, idMapperTarget, prefix);
        // filter revision of target entity
        Parameters parametersToUse = parameters;
        String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
        if (joinType == JoinType.LEFT) {
            parametersToUse = parameters.addSubParameters(Parameters.OR);
            parametersToUse.addNullRestriction(revisionPropertyPath, true);
            parametersToUse = parametersToUse.addSubParameters(Parameters.AND);
        }
        MiddleIdData referencedIdData = new MiddleIdData(verEntCfg, enversService.getEntitiesConfigurations().get(entityName).getIdMappingData(), null, entityName, enversService.getEntitiesConfigurations().isVersioned(entityName));
        enversService.getAuditStrategy().addEntityAtRevisionRestriction(enversService.getGlobalConfiguration(), queryBuilder, parametersToUse, revisionPropertyPath, verEntCfg.getRevisionEndFieldName(), true, referencedIdData, revisionPropertyPath, originalIdPropertyName, alias, queryBuilder.generateAlias(), true);
    } else {
        Parameters joinConditionParameters = queryBuilder.addJoin(joinType, entityName, alias, false);
        // owner.reference_id = target.id
        final IdMapper idMapperTarget = enversService.getEntitiesConfigurations().getNotVersionEntityConfiguration(entityName).getIdMapper();
        ownerAssociationIdMapper.addIdsEqualToQuery(joinConditionParameters, ownerAlias, idMapperTarget, alias);
    }
    for (AuditCriterion criterion : criterions) {
        criterion.addToQuery(enversService, versionsReader, aliasToEntityNameMap, alias, queryBuilder, parameters);
    }
    for (final AuditAssociationQueryImpl<?> sub : associationQueries) {
        sub.addCriterionsToQuery(versionsReader);
    }
}
Also used : Parameters(org.hibernate.envers.internal.tools.query.Parameters) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion)

Example 5 with AuditEntitiesConfiguration

use of org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration in project hibernate-orm by hibernate.

the class EntitiesAtRevisionQuery method list.

public List list() {
    /*
         * The query that we need to create:
         *   SELECT new list(e) FROM versionsReferencedEntity e
         *   WHERE
         * (all specified conditions, transformed, on the "e" entity) AND
         * (selecting e entities at revision :revision)
         *   --> for DefaultAuditStrategy:
         *     e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2
         *       WHERE e2.revision <= :revision AND e2.id = e.id) 
         *     
         *   --> for ValidityAuditStrategy:
         *     e.revision <= :revision and (e.endRevision > :revision or e.endRevision is null)
         *     
         *     AND
         * (only non-deleted entities)
         *     e.revision_type != DEL
         */
    AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration();
    String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
    String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
    MiddleIdData referencedIdData = new MiddleIdData(verEntCfg, enversService.getEntitiesConfigurations().get(entityName).getIdMappingData(), null, entityName, enversService.getEntitiesConfigurations().isVersioned(entityName));
    // (selecting e entities at revision :revision)
    // --> based on auditStrategy (see above)
    enversService.getAuditStrategy().addEntityAtRevisionRestriction(enversService.getGlobalConfiguration(), qb, qb.getRootParameters(), revisionPropertyPath, verEntCfg.getRevisionEndFieldName(), true, referencedIdData, revisionPropertyPath, originalIdPropertyName, REFERENCED_ENTITY_ALIAS, REFERENCED_ENTITY_ALIAS_DEF_AUD_STR, true);
    if (!includeDeletions) {
        // e.revision_type != DEL
        qb.getRootParameters().addWhereWithParam(verEntCfg.getRevisionTypePropName(), "<>", RevisionType.DEL);
    }
    // all specified conditions
    for (AuditCriterion criterion : criterions) {
        criterion.addToQuery(enversService, versionsReader, aliasToEntityNameMap, QueryConstants.REFERENCED_ENTITY_ALIAS, qb, qb.getRootParameters());
    }
    for (final AuditAssociationQueryImpl<?> associationQuery : associationQueries) {
        associationQuery.addCriterionsToQuery(versionsReader);
    }
    Query query = buildQuery();
    // add named parameter (used for ValidityAuditStrategy and association queries)
    Collection<String> params = query.getParameterMetadata().getNamedParameterNames();
    if (params.contains(REVISION_PARAMETER)) {
        query.setParameter(REVISION_PARAMETER, revision);
    }
    List queryResult = query.list();
    return applyProjections(queryResult, revision);
}
Also used : Query(org.hibernate.query.Query) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion) List(java.util.List)

Aggregations

AuditEntitiesConfiguration (org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration)8 AuditCriterion (org.hibernate.envers.query.criteria.AuditCriterion)4 List (java.util.List)3 Map (java.util.Map)3 HashMap (java.util.HashMap)2 MiddleIdData (org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData)2 Query (org.hibernate.query.Query)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ReflectionManager (org.hibernate.annotations.common.reflection.ReflectionManager)1 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)1 EntitiesConfigurator (org.hibernate.envers.configuration.internal.EntitiesConfigurator)1 GlobalConfiguration (org.hibernate.envers.configuration.internal.GlobalConfiguration)1 RevisionInfoConfiguration (org.hibernate.envers.configuration.internal.RevisionInfoConfiguration)1 RevisionInfoConfigurationResult (org.hibernate.envers.configuration.internal.RevisionInfoConfigurationResult)1 PersistentCollectionChangeData (org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 AuditProcessManager (org.hibernate.envers.internal.synchronization.AuditProcessManager)1 Parameters (org.hibernate.envers.internal.tools.query.Parameters)1 HibernateProxy (org.hibernate.proxy.HibernateProxy)1