Search in sources :

Example 1 with QueryBuilder

use of org.hibernate.envers.internal.tools.query.QueryBuilder in project hibernate-orm by hibernate.

the class AggregatedAuditExpression method addToQuery.

@Override
public void addToQuery(EnversService enversService, AuditReaderImplementor versionsReader, Map<String, String> aliasToEntityNameMap, String baseAlias, QueryBuilder qb, Parameters parameters) {
    String effectiveAlias = this.alias == null ? baseAlias : this.alias;
    String entityName = aliasToEntityNameMap.get(effectiveAlias);
    String propertyName = CriteriaTools.determinePropertyName(enversService, versionsReader, entityName, propertyNameGetter);
    CriteriaTools.checkPropertyNotARelation(enversService, entityName, propertyName);
    // Make sure our conditions are ANDed together even if the parent Parameters have a different connective
    Parameters subParams = parameters.addSubParameters(Parameters.AND);
    // This will be the aggregated query, containing all the specified conditions
    String auditEntityName = enversService.getAuditEntitiesConfiguration().getAuditEntityName(entityName);
    String subQueryAlias = qb.generateAlias();
    QueryBuilder subQb = qb.newSubQueryBuilder(auditEntityName, subQueryAlias);
    aliasToEntityNameMap.put(subQueryAlias, entityName);
    // aggregated one.
    for (AuditCriterion versionsCriteria : criterions) {
        versionsCriteria.addToQuery(enversService, versionsReader, aliasToEntityNameMap, effectiveAlias, qb, subParams);
        versionsCriteria.addToQuery(enversService, versionsReader, aliasToEntityNameMap, subQueryAlias, subQb, subQb.getRootParameters());
    }
    // Setting the desired projection of the aggregated query
    switch(mode) {
        case MIN:
            subQb.addProjection("min", subQb.getAlias(), propertyName, false);
            break;
        case MAX:
            subQb.addProjection("max", subQb.getAlias(), propertyName, false);
    }
    // Correlating subquery with the outer query by entity id. See JIRA HHH-7827.
    if (correlate) {
        final String originalIdPropertyName = enversService.getAuditEntitiesConfiguration().getOriginalIdPropName();
        enversService.getEntitiesConfigurations().get(entityName).getIdMapper().addIdsEqualToQuery(subQb.getRootParameters(), subQb.getRootAlias() + "." + originalIdPropertyName, effectiveAlias + "." + originalIdPropertyName);
    }
    // Adding the constrain on the result of the aggregated criteria
    subParams.addWhere(effectiveAlias, propertyName, "=", subQb);
}
Also used : Parameters(org.hibernate.envers.internal.tools.query.Parameters) QueryBuilder(org.hibernate.envers.internal.tools.query.QueryBuilder)

Example 2 with QueryBuilder

use of org.hibernate.envers.internal.tools.query.QueryBuilder in project hibernate-orm by hibernate.

the class DefaultAuditStrategy method addEntityAtRevisionRestriction.

public void addEntityAtRevisionRestriction(GlobalConfiguration globalCfg, QueryBuilder rootQueryBuilder, Parameters parameters, String revisionProperty, String revisionEndProperty, boolean addAlias, MiddleIdData idData, String revisionPropertyPath, String originalIdPropertyName, String alias1, String alias2, boolean inclusive) {
    // create a subquery builder
    // SELECT max(e.revision) FROM versionsReferencedEntity e2
    QueryBuilder maxERevQb = rootQueryBuilder.newSubQueryBuilder(idData.getAuditEntityName(), alias2);
    maxERevQb.addProjection("max", alias2, revisionPropertyPath, false);
    // WHERE
    Parameters maxERevQbParameters = maxERevQb.getRootParameters();
    // e2.revision <= :revision
    maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath, inclusive ? "<=" : "<", REVISION_PARAMETER);
    // e2.id_ref_ed = e.id_ref_ed
    idData.getOriginalMapper().addIdsEqualToQuery(maxERevQbParameters, alias1 + "." + originalIdPropertyName, alias2 + "." + originalIdPropertyName);
    // add subquery to rootParameters
    String subqueryOperator = globalCfg.getCorrelatedSubqueryOperator();
    parameters.addWhere(revisionProperty, addAlias, subqueryOperator, maxERevQb);
}
Also used : Parameters(org.hibernate.envers.internal.tools.query.Parameters) QueryBuilder(org.hibernate.envers.internal.tools.query.QueryBuilder)

Example 3 with QueryBuilder

use of org.hibernate.envers.internal.tools.query.QueryBuilder in project hibernate-orm by hibernate.

the class DefaultAuditStrategy method addAssociationAtRevisionRestriction.

public void addAssociationAtRevisionRestriction(QueryBuilder rootQueryBuilder, Parameters parameters, String revisionProperty, String revisionEndProperty, boolean addAlias, MiddleIdData referencingIdData, String versionsMiddleEntityName, String eeOriginalIdPropertyPath, String revisionPropertyPath, String originalIdPropertyName, String alias1, boolean inclusive, MiddleComponentData... componentDatas) {
    // SELECT max(ee2.revision) FROM middleEntity ee2
    QueryBuilder maxEeRevQb = rootQueryBuilder.newSubQueryBuilder(versionsMiddleEntityName, MIDDLE_ENTITY_ALIAS_DEF_AUD_STR);
    maxEeRevQb.addProjection("max", MIDDLE_ENTITY_ALIAS_DEF_AUD_STR, revisionPropertyPath, false);
    // WHERE
    Parameters maxEeRevQbParameters = maxEeRevQb.getRootParameters();
    // ee2.revision <= :revision
    maxEeRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, inclusive ? "<=" : "<", REVISION_PARAMETER);
    // ee2.originalId.* = ee.originalId.*
    String ee2OriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS_DEF_AUD_STR + "." + originalIdPropertyName;
    referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
    for (MiddleComponentData componentData : componentDatas) {
        componentData.getComponentMapper().addMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, alias1, ee2OriginalIdPropertyPath, MIDDLE_ENTITY_ALIAS_DEF_AUD_STR);
    }
    // add subquery to rootParameters
    parameters.addWhere(revisionProperty, addAlias, "=", maxEeRevQb);
}
Also used : Parameters(org.hibernate.envers.internal.tools.query.Parameters) QueryBuilder(org.hibernate.envers.internal.tools.query.QueryBuilder) MiddleComponentData(org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData)

Example 4 with QueryBuilder

use of org.hibernate.envers.internal.tools.query.QueryBuilder in project hibernate-orm by hibernate.

the class OneAuditEntityQueryGenerator method commonQueryPart.

/**
 * Compute common part for both queries.
 */
private QueryBuilder commonQueryPart(String versionsReferencedEntityName) {
    // SELECT e FROM versionsEntity e
    final QueryBuilder qb = new QueryBuilder(versionsReferencedEntityName, REFERENCED_ENTITY_ALIAS);
    qb.addProjection(null, REFERENCED_ENTITY_ALIAS, null, false);
    // WHERE
    if (multipleIdMapperKey) {
        // HHH-7625
        // support @OneToMany(mappedBy) to @ManyToOne @IdClass attribute.
        // e.originalId.id_ref_ed.id = :id_ref_ed
        final IdMapper mapper = getMultipleIdPrefixedMapper();
        mapper.addNamedIdEqualsToQuery(qb.getRootParameters(), null, referencingIdData.getPrefixedMapper(), true);
    } else {
        // e.id_ref_ed = :id_ref_ed
        referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(qb.getRootParameters(), null, true);
    }
    return qb;
}
Also used : IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) AbstractCompositeIdMapper(org.hibernate.envers.internal.entities.mapper.id.AbstractCompositeIdMapper) QueryBuilder(org.hibernate.envers.internal.tools.query.QueryBuilder)

Example 5 with QueryBuilder

use of org.hibernate.envers.internal.tools.query.QueryBuilder in project hibernate-orm by hibernate.

the class TwoEntityQueryGenerator method commonQueryPart.

/**
 * Compute common part for both queries.
 */
private QueryBuilder commonQueryPart(MiddleIdData referencedIdData, String versionsMiddleEntityName, String originalIdPropertyName) {
    final String eeOriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS + "." + originalIdPropertyName;
    // SELECT new list(ee) FROM middleEntity ee
    QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, MIDDLE_ENTITY_ALIAS);
    qb.addFrom(referencedIdData.getAuditEntityName(), REFERENCED_ENTITY_ALIAS, false);
    qb.addProjection("new list", MIDDLE_ENTITY_ALIAS + ", " + REFERENCED_ENTITY_ALIAS, null, false);
    // WHERE
    final Parameters rootParameters = qb.getRootParameters();
    // ee.id_ref_ed = e.id_ref_ed
    referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, referencedIdData.getOriginalMapper(), REFERENCED_ENTITY_ALIAS + "." + originalIdPropertyName);
    // ee.originalId.id_ref_ing = :id_ref_ing
    referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
    return qb;
}
Also used : Parameters(org.hibernate.envers.internal.tools.query.Parameters) QueryBuilder(org.hibernate.envers.internal.tools.query.QueryBuilder)

Aggregations

QueryBuilder (org.hibernate.envers.internal.tools.query.QueryBuilder)9 Parameters (org.hibernate.envers.internal.tools.query.Parameters)6 Map (java.util.Map)1 AbstractCompositeIdMapper (org.hibernate.envers.internal.entities.mapper.id.AbstractCompositeIdMapper)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 MiddleComponentData (org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData)1