Search in sources :

Example 1 with JoinSpecification

use of com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification in project midpoint by Evolveum.

the class ItemPathResolver method findExistingAlias.

private String findExistingAlias(RootHibernateQuery hibernateQuery, JpaLinkDefinition<?> joinedItemDefinition, String joinedItemFullPath) throws QueryException {
    for (JoinSpecification existingJoin : hibernateQuery.getPrimaryEntity().getJoinsFor(joinedItemFullPath)) {
        // but let's check the condition as well
        String existingAlias = existingJoin.getAlias();
        // we have to create condition for existing alias, to be matched to existing condition
        Condition conditionForExistingAlias = createJoinCondition(existingAlias, joinedItemDefinition, hibernateQuery);
        if (Objects.equals(conditionForExistingAlias, existingJoin.getCondition())) {
            LOGGER.trace("Reusing alias '{}' for joined path '{}'", existingAlias, joinedItemFullPath);
            return existingAlias;
        }
    }
    return null;
}
Also used : AndCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.AndCondition) Condition(com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition) JoinSpecification(com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification)

Example 2 with JoinSpecification

use of com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification in project midpoint by Evolveum.

the class ItemPathResolver method addTextInfoJoin.

public String addTextInfoJoin(String currentHqlPath) {
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    String joinedItemJpaName = RObject.F_TEXT_INFO_ITEMS;
    String joinedItemFullPath = currentHqlPath + "." + joinedItemJpaName;
    String joinedItemAlias = hibernateQuery.createAlias(joinedItemJpaName, false);
    hibernateQuery.getPrimaryEntity().addJoin(new JoinSpecification(joinedItemAlias, joinedItemFullPath, null));
    return joinedItemAlias;
}
Also used : RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) JoinSpecification(com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification)

Example 3 with JoinSpecification

use of com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification in project midpoint by Evolveum.

the class ItemPathResolver method reuseOrAddJoin.

String reuseOrAddJoin(JpaLinkDefinition<?> joinedItemDefinition, String currentHqlPath, boolean reuseMultivaluedJoins) throws QueryException {
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    String joinedItemJpaName = joinedItemDefinition.getJpaName();
    String joinedItemFullPath = currentHqlPath + "." + joinedItemJpaName;
    String joinedItemAlias;
    if (joinedItemDefinition.isMultivalued()) {
        if (reuseMultivaluedJoins) {
            String existingAlias = findExistingAlias(hibernateQuery, joinedItemDefinition, joinedItemFullPath);
            if (existingAlias != null) {
                return existingAlias;
            } else {
                // TODO better explanation
                throw new QueryException("Couldn't reuse existing join for " + joinedItemDefinition + " in the context of " + currentHqlPath);
            }
        }
    } else {
        /*
             * Let's check if we were already here i.e. if we had already created this join.
             * This is to avoid useless creation of redundant joins for singleton items.
             *
             * But how can we be sure that item is singleton if we look only at the last segment (which is single-valued)?
             * Imagine we are creating join for single-valued entity of u.abc.(...).xyz.ent where
             * ent is single-valued and not embedded (so we are to create something like "left join u.abc.(...).xyz.ent e").
             * Then "u" is certainly a single value: either the root object, or some value pointed to by Exists restriction.
             * Also, abc, ..., xyz are surely single-valued: otherwise they would be connected by a join. So,
             * u.abc.(...).xyz.ent is a singleton.
             *
             * Look at it in other way: if e.g. xyz was multivalued, then we would have something like:
             * left join u.abc.(...).uvw.xyz x
             * left join x.ent e
             * And, therefore we would not be looking for u.abc.(...).xyz.ent.
             */
        String existingAlias = findExistingAlias(hibernateQuery, joinedItemDefinition, joinedItemFullPath);
        if (existingAlias != null) {
            return existingAlias;
        }
    }
    joinedItemAlias = hibernateQuery.createAlias(joinedItemDefinition);
    Condition condition = createJoinCondition(joinedItemAlias, joinedItemDefinition, hibernateQuery);
    hibernateQuery.getPrimaryEntity().addJoin(new JoinSpecification(joinedItemAlias, joinedItemFullPath, condition));
    return joinedItemAlias;
}
Also used : AndCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.AndCondition) Condition(com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) JoinSpecification(com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification)

Aggregations

JoinSpecification (com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification)3 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery)2 AndCondition (com.evolveum.midpoint.repo.sql.query.hqm.condition.AndCondition)2 Condition (com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition)2 QueryException (com.evolveum.midpoint.repo.sqlbase.QueryException)1