Search in sources :

Example 6 with AuditEntitiesConfiguration

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

the class RevisionsOfEntityQuery method list.

@SuppressWarnings({ "unchecked" })
public List list() throws AuditException {
    AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration();
    /*
		The query that should be executed in the versions table:
        SELECT e (unless another projection is specified) FROM ent_ver e, rev_entity r WHERE
          e.revision_type != DEL (if selectDeletedEntities == false) AND
          e.revision = r.revision AND
          (all specified conditions, transformed, on the "e" entity)
          ORDER BY e.revision ASC (unless another order or projection is specified)
         */
    if (!selectDeletedEntities) {
        // e.revision_type != DEL AND
        qb.getRootParameters().addWhereWithParam(verEntCfg.getRevisionTypePropName(), "<>", RevisionType.DEL);
    }
    // all specified conditions, transformed
    for (AuditCriterion criterion : criterions) {
        criterion.addToQuery(enversService, versionsReader, aliasToEntityNameMap, QueryConstants.REFERENCED_ENTITY_ALIAS, qb, qb.getRootParameters());
    }
    if (!hasProjection() && !hasOrder) {
        String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
        qb.addOrder(QueryConstants.REFERENCED_ENTITY_ALIAS, revisionPropertyPath, true);
    }
    if (!selectEntitiesOnly) {
        qb.addFrom(enversService.getAuditEntitiesConfiguration().getRevisionInfoEntityName(), "r", true);
        qb.getRootParameters().addWhere(enversService.getAuditEntitiesConfiguration().getRevisionNumberPath(), true, "=", "r.id", false);
    }
    List<Object> queryResult = buildAndExecuteQuery();
    if (hasProjection()) {
        return queryResult;
    } else {
        List entities = new ArrayList();
        String revisionTypePropertyName = verEntCfg.getRevisionTypePropName();
        for (Object resultRow : queryResult) {
            Map versionsEntity;
            Object revisionData;
            if (selectEntitiesOnly) {
                versionsEntity = (Map) resultRow;
                revisionData = null;
            } else {
                Object[] arrayResultRow = (Object[]) resultRow;
                versionsEntity = (Map) arrayResultRow[0];
                revisionData = arrayResultRow[1];
            }
            Number revision = getRevisionNumber(versionsEntity);
            Object entity = entityInstantiator.createInstanceFromVersionsEntity(entityName, versionsEntity, revision);
            if (!selectEntitiesOnly) {
                entities.add(new Object[] { entity, revisionData, versionsEntity.get(revisionTypePropertyName) });
            } else {
                entities.add(entity);
            }
        }
        return entities;
    }
}
Also used : AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) ArrayList(java.util.ArrayList) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 7 with AuditEntitiesConfiguration

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

the class RevisionsOfEntityQuery method getRevisionNumber.

private Number getRevisionNumber(Map versionsEntity) {
    AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration();
    String originalId = verEntCfg.getOriginalIdPropName();
    String revisionPropertyName = verEntCfg.getRevisionFieldName();
    Object revisionInfoObject = ((Map) versionsEntity.get(originalId)).get(revisionPropertyName);
    if (revisionInfoObject instanceof HibernateProxy) {
        return (Number) ((HibernateProxy) revisionInfoObject).getHibernateLazyInitializer().getIdentifier();
    } else {
        // Not a proxy - must be read from cache or with a join
        return enversService.getRevisionInfoNumberReader().getRevisionNumber(revisionInfoObject);
    }
}
Also used : AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) Map(java.util.Map) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 8 with AuditEntitiesConfiguration

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

the class PersistentCollectionChangeWorkUnit method perform.

@Override
@SuppressWarnings({ "unchecked" })
public void perform(Session session, Object revisionData) {
    final AuditEntitiesConfiguration entitiesCfg = enversService.getAuditEntitiesConfiguration();
    for (PersistentCollectionChangeData persistentCollectionChangeData : collectionChanges) {
        // Setting the revision number
        ((Map<String, Object>) persistentCollectionChangeData.getData().get(entitiesCfg.getOriginalIdPropName())).put(entitiesCfg.getRevisionFieldName(), revisionData);
        auditStrategy.performCollectionChange(session, getEntityName(), referencingPropertyName, enversService, persistentCollectionChangeData, revisionData);
    }
}
Also used : AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) PersistentCollectionChangeData(org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData) HashMap(java.util.HashMap) Map(java.util.Map)

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