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 test340queryObjectClassTypeUser() throws Exception {
    Session session = open();/*from  ww w  .  j  a  v  a2  s  . c o  m*/

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

        main.add(Restrictions.eq("o." + RObject.F_OBJECT_TYPE_CLASS, RObjectType.USER));
        String expected = HibernateToSqlTranslator.toSql(main);

        TypeFilter type = TypeFilter.createType(UserType.COMPLEX_TYPE, null);
        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 test350queryObjectClassTypeAbstractRole() throws Exception {
    Session session = open();/*  ww  w.ja va  2s  .com*/

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

        List<RObjectType> list = new ArrayList<>();
        list.add(RObjectType.ABSTRACT_ROLE);
        list.add(RObjectType.ORG);
        list.add(RObjectType.ROLE);
        main.add(Restrictions.in("o." + RObject.F_OBJECT_TYPE_CLASS, list));
        String expected = HibernateToSqlTranslator.toSql(main);

        TypeFilter type = TypeFilter.createType(AbstractRoleType.COMPLEX_TYPE, null);
        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 test360queryMetadataTimestamp() throws Exception {
    Session session = open();/*from w ww .ja v  a2 s.  c o m*/

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

        XMLGregorianCalendar timeXml = XMLGregorianCalendarType.asXMLGregorianCalendar(new Date());

        main.add(Restrictions.le("r.createTimestamp", timeXml));
        String expected = HibernateToSqlTranslator.toSql(main);

        LessFilter less = LessFilter.createLess(
                new ItemPath(ReportOutputType.F_METADATA, MetadataType.F_CREATE_TIMESTAMP),
                ReportOutputType.class, prismContext, timeXml, true);

        String real = getInterpretedQuery(session, ReportOutputType.class, ObjectQuery.createObjectQuery(less));

        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 test370queryObjectypeByTypeUserAndLocality() throws Exception {
    Session session = open();/* w  w w  . j ava2  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));
        c.add(Restrictions.and(Restrictions.eq("o.localityUser.orig", "Caribbean"),
                Restrictions.eq("o.localityUser.norm", "caribbean")));

        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter eq = EqualFilter.createEqual(new ItemPath(UserType.F_LOCALITY), UserType.class,
                prismContext, new PolyString("Caribbean", "caribbean"));
        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);

        checkQueryTypeAlias(real, "m_user", "locality_orig", "locality_norm");
    } finally {
        close(session);
    }
}

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

License:Apache License

@Test
public void test375queryObjectypeByTypeOrgAndLocality() throws Exception {
    Session session = open();/*from  w  ww . j a v a2  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.ORG));
        c.add(Restrictions.and(Restrictions.eq("o.locality.orig", "Caribbean"),
                Restrictions.eq("o.locality.norm", "caribbean")));

        String expected = HibernateToSqlTranslator.toSql(main);

        EqualFilter eq = EqualFilter.createEqual(new ItemPath(OrgType.F_LOCALITY), OrgType.class, prismContext,
                new PolyString("Caribbean", "caribbean"));
        TypeFilter type = TypeFilter.createType(OrgType.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);

        checkQueryTypeAlias(real, "m_org", "locality_orig", "locality_norm");
    } 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   w  w  w. j  ava2 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();//www.  ja va2s  . 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  ww  .  ja  v  a2s  . c o 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.facade.AbstractFacade.java

public int count() {
    Transaction trans = null;//from w  w w  .j a va2s .c  o  m
    Session session = null;
    int cnt = 0;
    try {
        session = MyUtils.getSession();
        trans = session.beginTransaction();

        Criteria cq = session.createCriteria(entityClass);
        cq.setProjection(Projections.rowCount());
        cnt = ((Long) cq.uniqueResult()).intValue();

        trans.commit();
    } catch (Exception e) {
        try {
            if (trans != null && trans.isActive()) {
                trans.rollback();
            }
        } catch (Exception ex) {
            throw ex;
        }
        Logger.getLogger(entityClass.getName()).log(Level.SEVERE, null, e);
    } finally {
        MyUtils.closeSession(session);
    }
    return cnt;
}

From source file:com.fich.wafproject.dao.UserDaoImpl.java

public List<Users> findAll(int pageNumber, String[] targets, String[] names, String[] values,
        boolean pagination) {
    int pageSize = 4;
    Criteria crit = this.createEntityCriteria();
    crit.setProjection(Projections.distinct(Projections.property("id")));
    String dateFrom = "", dateTo = "", targetDate = "";
    if (names != null) {
        for (String alias : names) {
            crit.createAlias(alias, alias);
        }//from www.  jav  a2s  . c o m
    }
    int count = 0;
    if (values != null) {
        for (String value : values) {
            if (!value.equals("") && value != null) {
                if (targets[count].contains("date")) {
                    if (dateFrom != "") {
                        dateTo = value;
                    } else {
                        dateFrom = value;
                        targetDate = targets[count];
                    }
                } else {
                    crit.add(Restrictions.like(targets[count], "%" + value + "%"));
                }
            }
            count++;
        }
        if (targetDate != "") {
            if (dateFrom != "" && dateTo == "") {
                dateTo = dateFrom;
            }
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
            try {
                Date dateF = format.parse(dateFrom);
                Date dateT = format.parse(dateTo);
                crit.add(Restrictions.between(targetDate, dateF, dateT));
            } catch (ParseException ex) {
                Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    if (pagination) {
        crit.setFirstResult((pageNumber - 1) * pageSize);
        crit.setMaxResults(pageSize);
    }
    List<Users> users = new ArrayList<Users>();
    for (Object idEvent : crit.list()) {
        //            System.out.println(idEvent);
        users.add(this.findById((Long) idEvent));
    }
    return (List<Users>) users;
}