Example usage for org.hibernate Criteria setProjection

List of usage examples for org.hibernate Criteria setProjection

Introduction

In this page you can find the example usage for org.hibernate Criteria setProjection.

Prototype

public Criteria setProjection(Projection projection);

Source Link

Document

Used to specify that the query results will be a projection (scalar in nature).

Usage

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void queryTriggerTimestampDouble() throws Exception {
    final Date NOW = new Date();

    Session session = open();/*from   w  w  w  .  j a  v  a2 s .  c  om*/
    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("o", projections, false);
        main.setProjection(projections);

        Criteria d = main.createCriteria("trigger", "t", JoinType.LEFT_OUTER_JOIN);
        d.add(Restrictions.and(Restrictions.gt("t.timestamp", new Timestamp(NOW.getTime())),
                Restrictions.lt("t.timestamp", new Timestamp(NOW.getTime()))));

        String expected = HibernateToSqlTranslator.toSql(main);

        XMLGregorianCalendar thisScanTimestamp = XmlTypeConverter.createXMLGregorianCalendar(NOW.getTime());

        SchemaRegistry registry = prismContext.getSchemaRegistry();
        PrismObjectDefinition objectDef = registry.findObjectDefinitionByCompileTimeClass(ObjectType.class);
        ItemPath triggerPath = new ItemPath(ObjectType.F_TRIGGER, TriggerType.F_TIMESTAMP);
        //        PrismContainerDefinition triggerContainerDef = objectDef.findContainerDefinition(triggerPath);
        ObjectFilter greater = GreaterFilter.createGreater(triggerPath, objectDef, thisScanTimestamp, false);
        ObjectFilter lesser = LessFilter.createLess(triggerPath, objectDef, thisScanTimestamp, false);
        AndFilter and = AndFilter.createAnd(greater, lesser);
        LOGGER.info(and.debugDump());

        ObjectQuery query = ObjectQuery.createObjectQuery(and);
        String real = getInterpretedQuery(session, ObjectType.class, query);

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });
        AssertJUnit.assertEquals(expected, real);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void countObjectOrderByName() throws Exception {
    Session session = open();//from   w w  w. j  a v a 2 s  .c  o m

    try {
        Criteria main = session.createCriteria(RUser.class, "u");
        main.add(Restrictions.and(Restrictions.eq("u.name.orig", "cpt. Jack Sparrow"),
                Restrictions.eq("u.name.norm", "cpt jack sparrow")));
        main.setProjection(Projections.rowCount());
        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter filter = EqualFilter.createEqual(UserType.F_NAME, UserType.class, prismContext, null,
                new PolyString("cpt. Jack Sparrow", "cpt jack sparrow"));

        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        query.setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING));

        String real = getInterpretedQuery(session, UserType.class, query, true);

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });
        AssertJUnit.assertEquals(expected, real);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void countObjectOrderByNameWithoutFilter() throws Exception {
    Session session = open();/*from  w ww  . ja  v a 2s  .  co  m*/

    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        main.setProjection(Projections.rowCount());
        String expected = HibernateToSqlTranslator.toSql(main);

        ObjectPaging paging = ObjectPaging.createPaging(null, null, ObjectType.F_NAME,
                OrderDirection.ASCENDING);
        ObjectQuery query = ObjectQuery.createObjectQuery(null, paging);

        String real = getInterpretedQuery(session, ObjectType.class, query, true);

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });
        AssertJUnit.assertEquals(expected, real);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

/**
 * Q{AND: (EQUALS: parent, PPV(null)),PAGING: O: 0,M: 5,BY: name, D:ASCENDING,
 *
 * @throws Exception/*w ww. ja v  a 2 s .  co m*/
 */
@Test
public void countTaskOrderByName() throws Exception {
    Session session = open();

    try {
        Criteria main = session.createCriteria(RTask.class, "t");
        main.add(Restrictions.isNull("t.parent"));

        main.setProjection(Projections.rowCount());
        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter filter = EqualFilter.createEqual(TaskType.F_PARENT, TaskType.class, prismContext, null);

        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        query.setPaging(ObjectPaging.createPaging(null, null, TaskType.F_NAME, OrderDirection.ASCENDING));

        String real = getInterpretedQuery(session, TaskType.class, query, true);

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });
        AssertJUnit.assertEquals(expected, real);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void inOidTest() throws Exception {
    Session session = open();/* www.ja  va2  s  . c o m*/
    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        main.add(Restrictions.in("oid", Arrays.asList("1", "2")));
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("o", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);

        InOidFilter filter = InOidFilter.createInOid(Arrays.asList("1", "2"));

        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        String real = getInterpretedQuery(session, ObjectType.class, query, false);

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });
        AssertJUnit.assertEquals(expected, real);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void queryOrgTreeFindOrgs() throws Exception {
    Session session = open();//from  w  w w  . j av a 2  s .c om

    try {

        Criteria main = session.createCriteria(ROrg.class, "o");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("o", projections, false);
        main.setProjection(projections);

        DetachedCriteria detached = DetachedCriteria.forClass(RParentOrgRef.class, "p");
        detached.setProjection(Projections.distinct(Projections.property("p.ownerOid")));
        detached.add(Restrictions.eq("p.targetOid", "some oid"));

        main.add(Subqueries.propertyIn("o.oid", detached));
        main.addOrder(Order.asc("o.name.orig"));

        String expected = HibernateToSqlTranslator.toSql(main);

        OrgFilter orgFilter = OrgFilter.createOrg("some oid", OrgFilter.Scope.ONE_LEVEL);
        ObjectQuery objectQuery = ObjectQuery.createObjectQuery(orgFilter);
        objectQuery
                .setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING));

        String real = getInterpretedQuery(session, OrgType.class, objectQuery);

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });

        OperationResult result = new OperationResult("query org structure");
        repositoryService.searchObjects(OrgType.class, objectQuery, null, result);

        AssertJUnit.assertEquals(expected, real);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void asdf() throws Exception {
    Session session = open();/*from w  w w.ja v a2 s  . c  o m*/
    try {
        Criteria main = session.createCriteria(RUser.class, "u");
        Criteria a = main.createCriteria("assignments", "a");
        a.add(Restrictions.eq("a.assignmentOwner", RAssignmentOwner.FOCUS));
        Criteria e = a.createCriteria("a.extension");

        Criteria s = e.createCriteria("strings", "s");

        Conjunction c2 = Restrictions.conjunction();
        c2.add(Restrictions.eq("s.extensionType", RAssignmentExtensionType.EXTENSION));
        c2.add(Restrictions.eq("s.name", new QName("http://midpoint.evolveum.com/blabla", "foo")));
        c2.add(Restrictions.eq("s.value", "uid=jbond,ou=People,dc=example,dc=com"));

        Conjunction c1 = Restrictions.conjunction();
        c1.add(Restrictions.eq("a.targetRef.targetOid", "1234"));
        c1.add(Restrictions.eq("a.targetRef.type", RObjectType.ORG));

        main.add(Restrictions.and(c1, c2));

        main.setProjection(Projections.property("u.fullObject"));

        String expected = HibernateToSqlTranslator.toSql(main);
        LOGGER.info(">>> >>> {}", expected);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void test310QueryNameAndOrg() throws Exception {
    Session session = open();/*from   w w w  . java2 s.c  o m*/

    try {
        DetachedCriteria detached = DetachedCriteria.forClass(ROrgClosure.class, "cl");
        detached.setProjection(Projections.distinct(Projections.property("cl.descendantOid")));
        detached.add(Restrictions.eq("cl.ancestorOid", "1234"));
        detached.add(Restrictions.ne("cl.descendantOid", "1234"));

        Criteria main = session.createCriteria(RUser.class, "u");
        String mainAlias = "u";

        ProjectionList projections = Projections.projectionList();
        projections.add(Projections.property("fullObject"));

        projections.add(Projections.property("stringsCount"));
        projections.add(Projections.property("longsCount"));
        projections.add(Projections.property("datesCount"));
        projections.add(Projections.property("referencesCount"));
        projections.add(Projections.property("polysCount"));

        main.setProjection(projections);

        Conjunction c = Restrictions.conjunction();
        c.add(Restrictions.and(Restrictions.eq("u.name.orig", "cpt. Jack Sparrow"),
                Restrictions.eq("u.name.norm", "cpt jack sparrow")));
        c.add(Subqueries.propertyIn(mainAlias + ".oid", detached));
        main.add(c);

        main.addOrder(Order.asc("u.name.orig"));

        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter eqFilter = EqualFilter.createEqual(ObjectType.F_NAME, ObjectType.class, prismContext, null,
                new PolyString("cpt. Jack Sparrow", "cpt jack sparrow"));

        OrgFilter orgFilter = OrgFilter.createOrg("12341234-1234-1234-1234-123412341234");

        ObjectQuery query = ObjectQuery.createObjectQuery(AndFilter.createAnd(eqFilter, orgFilter));
        query.setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING));

        String real = getInterpretedQuery(session, UserType.class, query);

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });
        AssertJUnit.assertEquals(expected, real);
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test(enabled = false)
public void test320QueryEmployeeTypeAndOrgType() throws Exception {
    Session session = open();//from w  w  w .  j a  v  a2s  .c o m

    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        main.add(Restrictions.or(Restrictions.and(Restrictions.eq("o.name.orig", "some name"),
                Restrictions.eq("o.employeeNumber", "123")), Restrictions.eq("o.identifier", "1234")));
        ProjectionList list = Projections.projectionList();
        addFullObjectProjectionList("o", list, false);
        main.setProjection(list);

        List l = main.list();
        l.size();
        String expected = HibernateToSqlTranslator.toSql(main);
        LOGGER.info("expected query>\n{}", new Object[] { expected });

        //            EqualsFilter nameFilter = EqualsFilter.createEqual(ObjectType.F_NAME, ObjectType.class, prismContext,
        //                    null, new PolyString("cpt. Jack Sparrow", "cpt jack sparrow"));
        //
        //            EqualsFilter numberFilter = EqualsFilter.createEqual(UserType.F_EMPLOYEE_NUMBER, UserType.class, prismContext,
        //                    null, "123");
        //
        ////            EqualsFilter orgTypeFilter = EqualsFilter.createEqual(OrgType.F_ORG_TYPE, OrgType.class, prismContext,
        ////                    null, "orgtypevalue");
        //
        //            ObjectQuery query = ObjectQuery.createObjectQuery(OrFilter.createOr(
        //                    AndFilter.createAnd(nameFilter, numberFilter)//,
        ////                    orgTypeFilter
        //            ));
        //            query.setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING));
        //
        //            String real = getInterpretedQuery(session, ObjectType.class, query);
        //
        //            LOGGER.info("real query>\n{}", new Object[]{real});
    } finally {
        close(session);
    }
}

From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java

License:Apache License

@Test
public void test330queryUserSubstringName() throws Exception {
    Session session = open();//from www.j  av  a2 s . c om

    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("o", projections, false);
        main.setProjection(projections);

        main.add(Restrictions.like("name.orig", "a%"));
        String expected = HibernateToSqlTranslator.toSql(main);

        SubstringFilter substring = SubstringFilter.createSubstring(ObjectType.F_NAME, ObjectType.class,
                prismContext, PolyStringOrigMatchingRule.NAME, "a");
        substring.setAnchorStart(true);
        String real = getInterpretedQuery(session, ObjectType.class, ObjectQuery.createObjectQuery(substring));

        LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real });
        AssertJUnit.assertEquals(expected, real);

        OperationResult result = new OperationResult("test330queryUserSubstringName");
        int count = repositoryService.countObjects(ObjectType.class, ObjectQuery.createObjectQuery(substring),
                result);
        AssertJUnit.assertEquals(2, count);

        substring = SubstringFilter.createSubstring(ObjectType.F_NAME, ObjectType.class, prismContext,
                PolyStringOrigMatchingRule.NAME, "a");
        count = repositoryService.countObjects(ObjectType.class, ObjectQuery.createObjectQuery(substring),
                result);
        AssertJUnit.assertEquals(16, count);
    } finally {
        close(session);
    }
}