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 queryUserSubstringFullName() throws Exception {
    Session session = open();/*from  w  w  w  .jav  a 2  s.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.like("fullName.norm", "%cpt jack sparrow%").ignoreCase());
        String expected = HibernateToSqlTranslator.toSql(main);

        String real = getInterpretedQuery(session, UserType.class,
                new File(TEST_DIR, "query-user-substring-fullName.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 queryUserByName() throws Exception {
    Session session = open();/*w ww.j  a  va 2 s.c  o  m*/

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

        main.add(Restrictions.eq("name.norm", "some name identificator"));
        String expected = HibernateToSqlTranslator.toSql(main);

        String real = getInterpretedQuery(session, UserType.class,
                new File(TEST_DIR, "query-user-by-name.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 queryConnectorByType() throws Exception {
    Session session = open();//from  ww w  . jav  a 2  s .  co  m

    try {
        Criteria main = session.createCriteria(RConnector.class, "c");
        Criterion connectorType = Restrictions.conjunction()
                .add(Restrictions.eq("connectorType", "org.identityconnectors.ldap.LdapConnector"));
        main.add(connectorType);
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("c", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);

        String real = getInterpretedQuery(session, ConnectorType.class,
                new File(TEST_DIR, "query-connector-by-type.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 queryAccountByAttributesAndResourceRef() throws Exception {
    Session session = open();//  w ww  . j  a  va  2 s  . co m
    try {
        Criteria main = session.createCriteria(RShadow.class, "r");

        Criteria stringAttr = main.createCriteria("strings", "s1x", JoinType.LEFT_OUTER_JOIN);

        //and
        Conjunction c1 = Restrictions.conjunction();
        c1.add(Restrictions.eq("r.resourceRef.targetOid", "aae7be60-df56-11df-8608-0002a5d5c51b"));
        c1.add(Restrictions.eq("r.resourceRef.type", QNameUtil.qNameToUri(ResourceType.COMPLEX_TYPE)));
        //and
        Conjunction c2 = Restrictions.conjunction();
        c2.add(Restrictions.eq("s1x.ownerType", RObjectExtensionType.ATTRIBUTES));
        c2.add(Restrictions.eq("s1x.name", new QName("http://midpoint.evolveum.com/blabla", "foo")));
        c2.add(Restrictions.eq("s1x.value", "uid=jbond,ou=People,dc=example,dc=com"));

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

        String expected = HibernateToSqlTranslator.toSql(main);
        String real = getInterpretedQuery(session, ShadowType.class,
                new File(TEST_DIR, "query-account-by-attributes-and-resource-ref.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 queryUserAccountRef() throws Exception {
    Session session = open();// w w  w .j a va2s. c o  m
    try {
        Criteria main = session.createCriteria(RUser.class, "u");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("u", projections, false);
        main.setProjection(projections);

        Criteria refs = main.createCriteria("linkRef", "l", JoinType.LEFT_OUTER_JOIN);
        refs.add(Restrictions.conjunction().add(Restrictions.eq("l.targetOid", "123")));

        String expected = HibernateToSqlTranslator.toSql(main);

        RefFilter filter = RefFilter.createReferenceEqual(UserType.F_LINK_REF, UserType.class, prismContext,
                "123");
        String real = getInterpretedQuery(session, UserType.class, ObjectQuery.createObjectQuery(filter));

        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 queryTrigger() throws Exception {
    final Date NOW = new Date();

    Session session = open();/*from w  w  w.  j av  a  2s  . com*/
    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.le("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 filter = LessFilter.createLess(triggerPath, objectDef, thisScanTimestamp, true);
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        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 queryAssignmentActivationAdministrativeStatus() throws Exception {
    Session session = open();//from   w  w  w.  j  av  a 2 s  . c om
    try {
        Criteria main = session.createCriteria(RUser.class, "u");
        Criteria a = main.createCriteria("assignments", "a", JoinType.LEFT_OUTER_JOIN);
        a.add(Restrictions.and(Restrictions.eq("a.assignmentOwner", RAssignmentOwner.FOCUS),
                Restrictions.eq("a.activation.administrativeStatus", RActivationStatus.ENABLED)));
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("u", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);

        SchemaRegistry registry = prismContext.getSchemaRegistry();
        PrismObjectDefinition objectDef = registry.findObjectDefinitionByCompileTimeClass(UserType.class);
        ItemPath activationPath = new ItemPath(UserType.F_ASSIGNMENT, AssignmentType.F_ACTIVATION,
                ActivationType.F_ADMINISTRATIVE_STATUS);

        //        PrismContainerDefinition activationDef = objectDef.findContainerDefinition(activationPath);

        ObjectFilter filter = EqualFilter.createEqual(activationPath, objectDef, ActivationStatusType.ENABLED);
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        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 queryInducementActivationAdministrativeStatus() throws Exception {
    Session session = open();/* w  w  w . j a  va2 s.c o  m*/
    try {
        Criteria main = session.createCriteria(RRole.class, "r");
        Criteria a = main.createCriteria("assignments", "a", JoinType.LEFT_OUTER_JOIN);
        a.add(Restrictions.and(Restrictions.eq("a.assignmentOwner", RAssignmentOwner.ABSTRACT_ROLE),
                Restrictions.eq("a.activation.administrativeStatus", RActivationStatus.ENABLED)));
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("r", projections, false);
        main.setProjection(projections);

        String expected = HibernateToSqlTranslator.toSql(main);

        SchemaRegistry registry = prismContext.getSchemaRegistry();
        PrismObjectDefinition objectDef = registry.findObjectDefinitionByCompileTimeClass(RoleType.class);
        ItemPath activationPath = new ItemPath(RoleType.F_INDUCEMENT, AssignmentType.F_ACTIVATION,
                ActivationType.F_ADMINISTRATIVE_STATUS);

        //        PrismContainerDefinition activationDef = objectDef.findContainerDefinition(activationPath);

        ObjectFilter filter = EqualFilter.createEqual(activationPath, objectDef, ActivationStatusType.ENABLED);
        ObjectQuery query = ObjectQuery.createObjectQuery(filter);
        String real = getInterpretedQuery(session, RoleType.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 queryInducementAndAssignmentActivationAdministrativeStatus() throws Exception {
    Session session = open();//from w w  w  . j  av a 2s.  c o  m
    try {
        Criteria main = session.createCriteria(RRole.class, "r");
        Criteria a = main.createCriteria("assignments", "a", JoinType.LEFT_OUTER_JOIN);
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("r", projections, false);
        main.setProjection(projections);

        Criterion and1 = Restrictions.and(Restrictions.eq("a.assignmentOwner", RAssignmentOwner.FOCUS),
                Restrictions.eq("a.activation.administrativeStatus", RActivationStatus.ENABLED));

        Criterion and2 = Restrictions.and(Restrictions.eq("a.assignmentOwner", RAssignmentOwner.ABSTRACT_ROLE),
                Restrictions.eq("a.activation.administrativeStatus", RActivationStatus.ENABLED));

        a.add(Restrictions.or(and1, and2));

        String expected = HibernateToSqlTranslator.toSql(main);

        SchemaRegistry registry = prismContext.getSchemaRegistry();
        PrismObjectDefinition objectDef = registry.findObjectDefinitionByCompileTimeClass(RoleType.class);

        //filter1
        ItemPath activationPath1 = new ItemPath(UserType.F_ASSIGNMENT, AssignmentType.F_ACTIVATION,
                ActivationType.F_ADMINISTRATIVE_STATUS);
        //        PrismContainerDefinition activationDef1 = objectDef.findContainerDefinition(activationPath1);
        ObjectFilter filter1 = EqualFilter.createEqual(activationPath1, objectDef,
                ActivationStatusType.ENABLED);

        //filter2
        ItemPath activationPath2 = new ItemPath(RoleType.F_INDUCEMENT, AssignmentType.F_ACTIVATION,
                ActivationType.F_ADMINISTRATIVE_STATUS);
        //        PrismContainerDefinition activationDef2 = objectDef.findContainerDefinition(activationPath2);
        ObjectFilter filter2 = EqualFilter.createEqual(activationPath2, objectDef,
                ActivationStatusType.ENABLED);

        ObjectQuery query = ObjectQuery.createObjectQuery(OrFilter.createOr(filter1, filter2));
        String real = getInterpretedQuery(session, RoleType.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 queryUserByActivationDouble() throws Exception {
    Date NOW = new Date();

    Session session = open();//from  www  . j  av a 2 s  . com
    try {
        Criteria main = session.createCriteria(RUser.class, "u");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("u", projections, false);
        main.setProjection(projections);

        main.add(Restrictions.and(
                Restrictions.eq("u.activation.administrativeStatus", RActivationStatus.ENABLED),
                Restrictions.eq("u.activation.validFrom",
                        XmlTypeConverter.createXMLGregorianCalendar(NOW.getTime()))));

        String expected = HibernateToSqlTranslator.toSql(main);

        SchemaRegistry registry = prismContext.getSchemaRegistry();
        PrismObjectDefinition objectDef = registry.findObjectDefinitionByCompileTimeClass(UserType.class);
        //        ItemPath triggerPath = new ItemPath(AssignmentType.F_ACTIVATION);

        //        PrismContainerDefinition triggerContainerDef = objectDef.findContainerDefinition(triggerPath);

        ObjectFilter filter1 = EqualFilter.createEqual(
                new ItemPath(AssignmentType.F_ACTIVATION, ActivationType.F_ADMINISTRATIVE_STATUS), objectDef,
                ActivationStatusType.ENABLED);

        ObjectFilter filter2 = EqualFilter.createEqual(
                new ItemPath(AssignmentType.F_ACTIVATION, ActivationType.F_VALID_FROM), objectDef,
                XmlTypeConverter.createXMLGregorianCalendar(NOW.getTime()));

        ObjectQuery query = ObjectQuery.createObjectQuery(AndFilter.createAnd(filter1, filter2));
        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);
    }
}