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 queryOrganizationNorm() throws Exception {
    Session session = open();//from ww  w. ja va 2s . c om

    try {
        ObjectFilter filter = EqualFilter.createEqual(UserType.F_ORGANIZATION, UserType.class, prismContext,
                PolyStringNormMatchingRule.NAME, new PolyString("asdf", "asdf"));
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);

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

        Criteria o = main.createCriteria("organization", "o", JoinType.LEFT_OUTER_JOIN);

        o.add(Restrictions.eq("o.norm", "asdf"));

        String expected = HibernateToSqlTranslator.toSql(main);
        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
public void queryOrganizationOrig() throws Exception {
    Session session = open();/*  w w  w .j  a  v a2s  .  c  o  m*/
    try {
        ObjectFilter filter = EqualFilter.createEqual(UserType.F_ORGANIZATION, UserType.class, prismContext,
                PolyStringOrigMatchingRule.NAME, new PolyString("asdf", "asdf"));
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);

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

        Criteria o = main.createCriteria("organization", "o", JoinType.LEFT_OUTER_JOIN);

        o.add(Restrictions.eq("o.orig", "asdf"));

        String expected = HibernateToSqlTranslator.toSql(main);
        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
public void queryOrganizationStrict() throws Exception {
    Session session = open();//from  w ww  . j  av a 2s.c  o  m
    try {
        ObjectFilter filter = EqualFilter.createEqual(UserType.F_ORGANIZATION, UserType.class, prismContext,
                null, new PolyString("asdf", "asdf"));
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);

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

        Criteria o = main.createCriteria("organization", "o", JoinType.LEFT_OUTER_JOIN);

        o.add(Restrictions.conjunction().add(Restrictions.eq("o.orig", "asdf"))
                .add(Restrictions.eq("o.norm", "asdf")));

        String expected = HibernateToSqlTranslator.toSql(main);
        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
public void queryDependent() throws Exception {
    Session session = open();/*from   w  ww. j av  a  2s  .  c  o m*/

    try {
        Criteria main = session.createCriteria(RTask.class, "t");
        Criteria d = main.createCriteria("dependent", "d", JoinType.LEFT_OUTER_JOIN);
        d.add(Restrictions.eq("d.elements", "123456"));
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("t", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);

        ObjectFilter filter = EqualFilter.createEqual(TaskType.F_DEPENDENT, TaskType.class, prismContext, null,
                "123456");
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        String real = getInterpretedQuery(session, TaskType.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 queryEnum() throws Exception {
    Session session = open();/*from   ww w  .  j  a  v  a  2  s  .co m*/
    try {
        Criteria main = session.createCriteria(RTask.class, "t");
        main.add(Restrictions.eq("executionStatus", RTaskExecutionStatus.WAITING));
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("t", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);

        ObjectFilter filter = EqualFilter.createEqual(TaskType.F_EXECUTION_STATUS, TaskType.class, prismContext,
                null, TaskExecutionStatusType.WAITING);
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        String real = getInterpretedQuery(session, TaskType.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 queryEnabled() throws Exception {
    Session session = open();/*from w ww  .  j ava2  s  .c o  m*/
    try {
        Criteria main = session.createCriteria(RUser.class, "u");
        main.add(Restrictions.eq("activation.administrativeStatus", RActivationStatus.ENABLED));
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("u", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);
        String real = getInterpretedQuery(session, UserType.class,
                new File(TEST_DIR, "query-user-by-enabled.xml"));

        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 queryGenericLong() throws Exception {
    Session session = open();/*from   w ww.  j  ava 2s  .  c  o  m*/
    try {
        Criteria main = session.createCriteria(RGenericObject.class, "g");

        Criteria stringExt = main.createCriteria("longs", "l", JoinType.LEFT_OUTER_JOIN);

        //and
        Criterion c1 = Restrictions.eq("name.norm", "generic object");
        //and
        Conjunction c2 = Restrictions.conjunction();
        c2.add(Restrictions.eq("l.ownerType", RObjectExtensionType.EXTENSION));
        c2.add(Restrictions.eq("l.name", new QName("http://example.com/p", "intType")));
        c2.add(Restrictions.eq("l.value", 123L));

        Conjunction conjunction = Restrictions.conjunction();
        conjunction.add(c1);
        conjunction.add(c2);
        main.add(conjunction);
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("g", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);
        String real = getInterpretedQuery(session, GenericObjectType.class,
                new File(TEST_DIR, "query-and-generic.xml"));

        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 queryOrComposite() throws Exception {
    Session session = open();/*w  ww .  j a  v a  2  s  .  co m*/
    try {
        Criteria main = session.createCriteria(RShadow.class, "r");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("r", projections, false);
        main.setProjection(projections);

        Criteria stringExt = main.createCriteria("strings", "s1", JoinType.LEFT_OUTER_JOIN);

        //or
        Criterion c1 = Restrictions.eq("intent", "some account type");
        //or
        Conjunction c2 = Restrictions.conjunction();
        c2.add(Restrictions.eq("s1.ownerType", RObjectExtensionType.ATTRIBUTES));
        c2.add(Restrictions.eq("s1.name", new QName("http://midpoint.evolveum.com/blabla", "foo")));
        c2.add(Restrictions.eq("s1.value", "foo value"));
        //or
        Conjunction c3 = Restrictions.conjunction();
        c3.add(Restrictions.eq("s1.ownerType", RObjectExtensionType.EXTENSION));
        c3.add(Restrictions.eq("s1.name", new QName("http://example.com/p", "stringType")));
        c3.add(Restrictions.eq("s1.value", "uid=test,dc=example,dc=com"));
        //or
        Conjunction c4 = Restrictions.conjunction();
        c4.add(Restrictions.eq("r.resourceRef.targetOid", "d0db5be9-cb93-401f-b6c1-86ffffe4cd5e"));
        c4.add(Restrictions.eq("r.resourceRef.type", QNameUtil.qNameToUri(ResourceType.COMPLEX_TYPE)));

        Disjunction disjunction = Restrictions.disjunction();
        disjunction.add(c1);
        disjunction.add(c2);
        disjunction.add(c3);
        disjunction.add(c4);
        main.add(disjunction);

        String expected = HibernateToSqlTranslator.toSql(main);
        String real = getInterpretedQuery(session, ShadowType.class,
                new File(TEST_DIR, "query-or-composite.xml"));

        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 queryObjectByName() throws Exception {
    Session session = open();//  ww w  . j av  a 2s . c o  m

    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        main.add(Restrictions.and(Restrictions.eq("name.orig", "cpt. Jack Sparrow"),
                Restrictions.eq("name.norm", "cpt jack sparrow")));
        main.addOrder(Order.asc("name.orig"));
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("o", projections, false);
        main.setProjection(projections);
        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter filter = EqualFilter.createEqual(ObjectType.F_NAME, ObjectType.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, 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 queryUserByFullName() throws Exception {
    Session session = open();//from  w  w w  . j a  v a  2s . c  o  m

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

        main.add(Restrictions.eq("fullName.norm", "cpt jack sparrow"));
        String expected = HibernateToSqlTranslator.toSql(main);

        String real = getInterpretedQuery(session, UserType.class,
                new File(TEST_DIR, "query-user-by-fullName.xml"));

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