Search in sources :

Example 51 with Query

use of org.hibernate.query.Query in project midpoint by Evolveum.

the class AbstractOrgClosureTest method _test410RandomUnloadOrgStructure.

protected void _test410RandomUnloadOrgStructure() throws Exception {
    OperationResult opResult = new OperationResult("===[ test410RandomUnloadOrgStructure ]===");
    long start = System.currentTimeMillis();
    randomRemoveOrgStructure(opResult);
    System.out.println("Removed in " + (System.currentTimeMillis() - start) + " ms");
    try (Session session = openSession()) {
        Query q = session.createNativeQuery("select count(*) from m_org_closure");
        Object count = q.list().get(0);
        System.out.println("OrgClosure table has " + count + " rows");
        assertEquals("Closure is not empty", "0", count.toString());
    }
    logger.info("Finish.");
}
Also used : Query(org.hibernate.query.Query) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PrismObject(com.evolveum.midpoint.prism.PrismObject) Session(org.hibernate.Session)

Example 52 with Query

use of org.hibernate.query.Query in project midpoint by Evolveum.

the class AbstractOrgClosureTest method getOrgChildren.

private List<String> getOrgChildren(String oid) {
    try (Session session = openSession()) {
        Query childrenQuery = session.createQuery("select distinct parentRef.ownerOid from RObjectReference as parentRef" + " join parentRef.owner as owner where parentRef.targetOid=:oid and parentRef.referenceType=0" + " and owner.objectTypeClass = :orgType");
        // TODO eliminate use of parameter here
        childrenQuery.setParameter("orgType", RObjectType.ORG);
        childrenQuery.setParameter("oid", oid);
        // noinspection unchecked
        return childrenQuery.list();
    }
}
Also used : Query(org.hibernate.query.Query) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) Session(org.hibernate.Session)

Example 53 with Query

use of org.hibernate.query.Query in project midpoint by Evolveum.

the class ObjectRetriever method searchShadowOwnerAttempt.

public <F extends FocusType> PrismObject<F> searchShadowOwnerAttempt(String shadowOid, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) {
    LOGGER_PERFORMANCE.debug("> search shadow owner for oid={}", shadowOid);
    PrismObject<F> owner = null;
    Session session = null;
    try {
        session = baseHelper.beginReadOnlyTransaction();
        LOGGER.trace("Selecting account shadow owner for account {}.", shadowOid);
        Query query = session.getNamedQuery("searchShadowOwner.getOwner");
        query.setParameter("oid", shadowOid);
        query.setResultTransformer(GetObjectResult.RESULT_STYLE.getResultTransformer());
        @SuppressWarnings({ "unchecked", "raw" }) List<GetObjectResult> focuses = query.list();
        LOGGER.trace("Found {} focuses, transforming data to JAXB types.", focuses != null ? focuses.size() : 0);
        if (focuses == null || focuses.isEmpty()) {
            // account shadow owner was not found
            return null;
        } else if (focuses.size() > 1) {
            LOGGER.warn("Found {} owners for shadow oid {}, returning first owner.", focuses.size(), shadowOid);
        }
        GetObjectResult focus = focuses.get(0);
        owner = updateLoadedObject(focus, (Class<F>) FocusType.class, null, options, null, session);
        session.getTransaction().commit();
    } catch (SchemaException | RuntimeException | ObjectNotFoundException ex) {
        baseHelper.handleGeneralException(ex, session, result);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
    }
    return owner;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Query(org.hibernate.query.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) NativeQuery(org.hibernate.query.NativeQuery) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 54 with Query

use of org.hibernate.query.Query in project midpoint by Evolveum.

the class ObjectRetriever method isAnySubordinateAttempt.

public boolean isAnySubordinateAttempt(String upperOrgOid, Collection<String> lowerObjectOids) {
    Session session = null;
    try {
        session = baseHelper.beginReadOnlyTransaction();
        Query query;
        if (lowerObjectOids.size() == 1) {
            query = session.getNamedQuery("isAnySubordinateAttempt.oneLowerOid");
            query.setParameter("dOid", lowerObjectOids.iterator().next());
        } else {
            query = session.getNamedQuery("isAnySubordinateAttempt.moreLowerOids");
            query.setParameterList("dOids", lowerObjectOids);
        }
        query.setParameter("aOid", upperOrgOid);
        Number number = (Number) query.uniqueResult();
        session.getTransaction().commit();
        return number != null && number.longValue() != 0L;
    } catch (RuntimeException ex) {
        baseHelper.handleGeneralException(ex, session, null);
    } finally {
        baseHelper.cleanupSessionAndResult(session, null);
    }
    throw new SystemException("isAnySubordinateAttempt failed somehow, this really should not happen.");
}
Also used : Query(org.hibernate.query.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) NativeQuery(org.hibernate.query.NativeQuery) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Example 55 with Query

use of org.hibernate.query.Query in project midpoint by Evolveum.

the class LookupTableHelper method updateLoadedLookupTable.

<T extends ObjectType> void updateLoadedLookupTable(PrismObject<T> object, Collection<SelectorOptions<GetOperationOptions>> options, Session session) throws SchemaException {
    if (!SelectorOptions.hasToLoadPath(LookupTableType.F_ROW, options)) {
        return;
    }
    LOGGER.debug("Loading lookup table data.");
    GetOperationOptions getOption = findLookupTableGetOption(options);
    RelationalValueSearchQuery queryDef = getOption == null ? null : getOption.getRelationalValueSearchQuery();
    Query query = setupLookupTableRowsQuery(session, queryDef, object.getOid());
    if (queryDef != null && queryDef.getPaging() != null) {
        ObjectPaging paging = queryDef.getPaging();
        if (paging.getOffset() != null) {
            query.setFirstResult(paging.getOffset());
        }
        if (paging.getMaxSize() != null) {
            query.setMaxResults(paging.getMaxSize());
        }
    }
    // noinspection unchecked
    List<RLookupTableRow> rows = query.list();
    if (CollectionUtils.isNotEmpty(rows)) {
        LookupTableType lookup = (LookupTableType) object.asObjectable();
        List<LookupTableRowType> jaxbRows = lookup.getRow();
        for (RLookupTableRow row : rows) {
            LookupTableRowType jaxbRow = row.toJAXB(prismContext);
            jaxbRows.add(jaxbRow);
        }
        PrismContainer<LookupTableRowType> rowContainer = object.findContainer(LookupTableType.F_ROW);
        rowContainer.setIncomplete(false);
    } else {
        PrismContainer<LookupTableRowType> rowContainer = object.findContainer(LookupTableType.F_ROW);
        if (rowContainer != null) {
            // just in case
            rowContainer.clear();
            rowContainer.setIncomplete(false);
        }
    }
}
Also used : RLookupTableRow(com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) ObjectPaging(com.evolveum.midpoint.prism.query.ObjectPaging) Query(org.hibernate.query.Query) RelationalValueSearchQuery(com.evolveum.midpoint.schema.RelationalValueSearchQuery) RelationalValueSearchQuery(com.evolveum.midpoint.schema.RelationalValueSearchQuery) LookupTableRowType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType) LookupTableType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType)

Aggregations

Query (org.hibernate.query.Query)149 Test (org.junit.Test)46 Session (org.hibernate.Session)39 List (java.util.List)24 ArrayList (java.util.ArrayList)19 TestForIssue (org.hibernate.testing.TestForIssue)19 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)17 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)17 HashMap (java.util.HashMap)15 Map (java.util.Map)14 NativeQuery (org.hibernate.query.NativeQuery)14 AbstractJPATest (org.hibernate.test.jpa.AbstractJPATest)11 SessionFactory (org.hibernate.SessionFactory)9 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)7 SQLException (java.sql.SQLException)7 Collectors (java.util.stream.Collectors)7 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 Predicate (javax.persistence.criteria.Predicate)6 Root (javax.persistence.criteria.Root)6