Search in sources :

Example 1 with AuditCriterion

use of org.hibernate.envers.query.criteria.AuditCriterion in project hibernate-orm by hibernate.

the class StoreDeletedData method testMaximizeInDisjunction.

@Test
@TestForIssue(jiraKey = "HHH-7800")
public void testMaximizeInDisjunction() {
    List<Integer> queryIds = Arrays.asList(id2, id3);
    AuditDisjunction disjunction = AuditEntity.disjunction();
    for (Integer id : queryIds) {
        AuditCriterion crit = AuditEntity.revisionNumber().maximize().add(AuditEntity.id().eq(id)).add(AuditEntity.revisionType().ne(RevisionType.DEL));
        disjunction.add(crit);
    // Workaround: using this line instead works correctly:
    // disjunction.add(AuditEntity.conjunction().add(crit));
    }
    List<?> beforeDeletionRevisions = getAuditReader().createQuery().forRevisionsOfEntity(StrIntTestEntity.class, false, false).add(disjunction).addOrder(AuditEntity.property("id").asc()).getResultList();
    Assert.assertEquals(2, beforeDeletionRevisions.size());
    Object[] result1 = (Object[]) beforeDeletionRevisions.get(0);
    Object[] result2 = (Object[]) beforeDeletionRevisions.get(1);
    Assert.assertEquals(new StrIntTestEntity("b", 20, id2), result1[0]);
    // Making sure that we have received an entity added at revision 3.
    Assert.assertEquals(3, ((SequenceIdRevisionEntity) result1[1]).getId());
    Assert.assertEquals(RevisionType.ADD, result1[2]);
    Assert.assertEquals(new StrIntTestEntity("c", 30, id3), result2[0]);
    // Making sure that we have received an entity added at revision 3.
    Assert.assertEquals(3, ((SequenceIdRevisionEntity) result2[1]).getId());
    Assert.assertEquals(RevisionType.ADD, result2[2]);
}
Also used : StrIntTestEntity(org.hibernate.envers.test.entities.StrIntTestEntity) AuditDisjunction(org.hibernate.envers.query.criteria.AuditDisjunction) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with AuditCriterion

use of org.hibernate.envers.query.criteria.AuditCriterion 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();
    // 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) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion) List(java.util.List)

Example 3 with AuditCriterion

use of org.hibernate.envers.query.criteria.AuditCriterion 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 4 with AuditCriterion

use of org.hibernate.envers.query.criteria.AuditCriterion 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)

Example 5 with AuditCriterion

use of org.hibernate.envers.query.criteria.AuditCriterion 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);
    }
    return getQueryResults();
}
Also used : AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion)

Aggregations

AuditCriterion (org.hibernate.envers.query.criteria.AuditCriterion)5 AuditEntitiesConfiguration (org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration)4 List (java.util.List)2 MiddleIdData (org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData)2 Query (org.hibernate.query.Query)2 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 Parameters (org.hibernate.envers.internal.tools.query.Parameters)1 AuditDisjunction (org.hibernate.envers.query.criteria.AuditDisjunction)1 StrIntTestEntity (org.hibernate.envers.test.entities.StrIntTestEntity)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.Test)1