Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

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

Prototype

@Deprecated
public Criteria createCriteria(String associationPath, String alias, int joinType) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity, assigning the given alias and using the specified join type.

Usage

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

License:Apache License

@Test
public void queryUserAccountRef() throws Exception {
    Session session = open();//from  ww  w .j  a v a2 s  .  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();//  w w  w.j  a va2s . co  m
    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.jav a  2 s. co  m
    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  v a 2 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 ww . j  a  v  a 2s .com*/
    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 queryTriggerTimestampDouble() throws Exception {
    final Date NOW = new Date();

    Session session = open();//from   w  ww.  jav a 2  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 test380queryObjectypeByTypeAndExtensionAttribute() throws Exception {
    Session session = open();//from   ww w . ja  va2 s  .c om
    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("o", projections, false);
        main.setProjection(projections);

        Conjunction c = Restrictions.conjunction();
        main.add(c);
        c.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.USER));

        Conjunction c2 = Restrictions.conjunction();
        main.createCriteria("strings", "s", JoinType.LEFT_OUTER_JOIN);
        c2.add(Restrictions.eq("s.ownerType", RObjectExtensionType.EXTENSION));
        c2.add(Restrictions.eq("s.name", new QName("http://example.com/p", "weapon")));
        c2.add(Restrictions.eq("s.value", "some weapon name"));
        c.add(c2);

        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter eq = EqualFilter.createEqual(
                new ItemPath(ObjectType.F_EXTENSION, new QName("http://example.com/p", "weapon")),
                UserType.class, prismContext, "some weapon name");
        TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, eq);

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

        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 test390queryObjectypeByTypeAndReference() throws Exception {
    Session session = open();/*from w ww .java  2  s  .co  m*/
    try {
        Criteria main = session.createCriteria(RObject.class, "o");
        ProjectionList projections = Projections.projectionList();
        addFullObjectProjectionList("o", projections, false);
        main.setProjection(projections);

        Conjunction c = Restrictions.conjunction();
        main.add(c);
        c.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.USER));

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

        String expected = HibernateToSqlTranslator.toSql(main);

        RefFilter ref = RefFilter.createReferenceEqual(UserType.F_LINK_REF, UserType.class, prismContext,
                "123");
        TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, ref);

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

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

        Conjunction c1 = Restrictions.conjunction();
        c1.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.USER));
        Criterion e1 = Restrictions.and(Restrictions.eq("o.localityUser.orig", "Caribbean"),
                Restrictions.eq("o.localityUser.norm", "caribbean"));
        Criterion e2 = Restrictions.and(Restrictions.eq("o.localityUser.orig", "Adriatic"),
                Restrictions.eq("o.localityUser.norm", "adriatic"));
        c1.add(Restrictions.or(e1, e2));

        Conjunction c2 = Restrictions.conjunction();
        c2.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.ORG));
        Criteria o1 = main.createCriteria("o.orgType", "o1", JoinType.LEFT_OUTER_JOIN);
        c2.add(Restrictions.eq("o1.elements", "functional"));

        Criterion c3 = Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.REPORT);

        main.add(Restrictions.or(c1, c2, c3));
        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter eq1 = EqualFilter.createEqual(UserType.F_LOCALITY, UserType.class, prismContext,
                new PolyString("Caribbean", "caribbean"));
        EqualFilter eq2 = EqualFilter.createEqual(UserType.F_LOCALITY, UserType.class, prismContext,
                new PolyString("Adriatic", "adriatic"));
        TypeFilter type1 = TypeFilter.createType(UserType.COMPLEX_TYPE, OrFilter.createOr(eq1, eq2));

        EqualFilter equal = EqualFilter.createEqual(OrgType.F_ORG_TYPE, OrgType.class, prismContext,
                "functional");
        TypeFilter type2 = TypeFilter.createType(OrgType.COMPLEX_TYPE, equal);

        TypeFilter type3 = TypeFilter.createType(ReportType.COMPLEX_TYPE, null);

        OrFilter or = OrFilter.createOr(type1, type2, type3);

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

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

From source file:com.krawler.common.util.BuildCriteria.java

License:Open Source License

private static void buildCriteria(Object value, Integer conditionval, Criteria crit, String association,
        HashMap<String, String> associationparams, HashMap<String, Criteria> existedassociationparams) {
    if (association != null) {
        String[] associationArr = association.split("\\.");
        if (associationArr.length >= 2) {
            Criteria parentcrit = null;
            for (int i = 0; i < associationArr.length - 1; i++) { // -1 is to ignore property from association

                if (parentcrit == null && existedassociationparams.containsKey(associationArr[i])) {
                    parentcrit = existedassociationparams.get(associationArr[i]);
                } else {
                    if (parentcrit != null) {
                        parentcrit = parentcrit.createCriteria(associationArr[i], associationArr[i],
                                Criteria.LEFT_JOIN);
                    } else {
                        parentcrit = crit.createCriteria(associationArr[i], associationArr[i],
                                Criteria.LEFT_JOIN);
                    }//from  ww w  .jav  a2s.com
                }

                if (i == associationArr.length - 2) {
                    associationparams.put(association, associationArr[i] + "." + associationArr[i + 1]);
                    pushCriteriaValue(value, conditionval, parentcrit, associationArr[i + 1]);
                }
                existedassociationparams.put(associationArr[i], parentcrit);
            }
        } else {
            pushCriteriaValue(value, conditionval, crit, association);
        }
    }

}