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

public Criteria createCriteria(String associationPath) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity.

Usage

From source file:gov.nih.nci.security.upt.util.HibernateHelper.java

License:BSD License

private static Criteria createCriterias(FilterClause filterClause, Session session) {
    List<Criteria> criteriaList = new ArrayList();
    criteriaList.add(0, session.createCriteria(filterClause.getClassName()));
    StringTokenizer stringTokenizer = new StringTokenizer(filterClause.getFilterChain(), ",");
    int count = 0;
    while (stringTokenizer.hasMoreTokens()) {
        String attributeName = stringTokenizer.nextToken();
        if (attributeName.trim().equals(filterClause.getClassName()))
            break;
        count++;//  w  w w  .  j a va2 s .  c  o  m
        Criteria parentCriteria = criteriaList.get(count - 1);
        Criteria childCriteria = parentCriteria.createCriteria(attributeName.trim());
        criteriaList.add(count, childCriteria);
    }
    Criteria targetCriteria = criteriaList.get(count);
    String attributeName = filterClause.getTargetClassAttributeName();
    Class attributeType = null;
    Class IntegerType = null;
    try {
        attributeType = Class.forName(filterClause.getTargetClassAttributeType());
        IntegerType = Class.forName("java.lang.Integer");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Object valueArray = Array.newInstance(attributeType, 1);

    try {
        if (attributeType.equals(IntegerType)) {
            Array.set(valueArray, 0, new Integer(0));
        } else {
            Array.set(valueArray, 0, attributeType.newInstance());
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    targetCriteria.add(Expression.in(attributeName, (Object[]) valueArray));
    Criteria mainCriteria = (Criteria) criteriaList.get(0);
    mainCriteria.setProjection(Projections.id());

    return mainCriteria;
}

From source file:gr.abiss.calipso.hibernate.HibernateDao.java

License:Open Source License

@Override
public int loadCountOfRecordsHavingFieldNotNull(Space space, Field field) {
    Criteria criteria = getSession().createCriteria(Item.class);
    criteria.add(Restrictions.eq("space", space));
    criteria.add(Restrictions.isNotNull(field.getName().toString()));
    criteria.setProjection(Projections.rowCount());
    int itemCount = NumberUtils.toInt(criteria.list().get(0).toString());
    // even when no item has this field not null currently, items may have history with this field not null
    // because of the "parent" difference, cannot use AbstractItem and have to do a separate Criteria query
    criteria = getSession().createCriteria(History.class);
    criteria.createCriteria("parent").add(Restrictions.eq("space", space));
    criteria.add(Restrictions.isNotNull(field.getName().toString()));
    criteria.setProjection(Projections.rowCount());
    return itemCount + NumberUtils.toInt(criteria.list().get(0).toString());
}

From source file:gr.abiss.calipso.hibernate.HibernateDao.java

License:Open Source License

@Override
public int loadCountOfRecordsHavingFieldWithValue(Space space, Field field, int optionKey) {
    Criteria criteria = getSession().createCriteria(Item.class);
    criteria.add(Restrictions.eq("space", space));
    criteria.add(Restrictions.eq(field.getName().toString(), optionKey));
    criteria.setProjection(Projections.rowCount());
    int itemCount = NumberUtils.toInt(criteria.list().get(0).toString());
    // even when no item has this field value currently, items may have history with this field value
    // because of the "parent" difference, cannot use AbstractItem and have to do a separate Criteria query
    criteria = getSession().createCriteria(History.class);
    criteria.createCriteria("parent").add(Restrictions.eq("space", space));
    criteria.add(Restrictions.eq(field.getName().toString(), optionKey));
    criteria.setProjection(Projections.rowCount());
    return itemCount + NumberUtils.toInt(criteria.list().get(0).toString());
}

From source file:gr.abiss.calipso.hibernate.HibernateDao.java

License:Open Source License

@Override
public int loadCountOfRecordsHavingStatus(Space space, int status) {
    Criteria criteria = getSession().createCriteria(Item.class);
    criteria.add(Restrictions.eq("space", space));
    criteria.add(Restrictions.eq("status", status));
    criteria.setProjection(Projections.rowCount());
    Long itemCount = (Long) criteria.list().get(0);
    // even when no item has this status currently, items may have history with this status
    // because of the "parent" difference, cannot use AbstractItem and have to do a separate Criteria query
    criteria = getSession().createCriteria(History.class);
    criteria.createCriteria("parent").add(Restrictions.eq("space", space));
    criteria.add(Restrictions.eq("status", status));
    criteria.setProjection(Projections.rowCount());
    return itemCount.intValue() + ((Long) criteria.list().get(0)).intValue();
}

From source file:helpers.OlymHelper.java

public static List<Sportista> searchSportists(String sportista, Zemlja zemlja, Sport sport, int disciplina,
        String gender, boolean medal) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tr = null;/*from  w ww . java  2s.  c  o  m*/
    List<Sportista> sportists = null;
    try {
        tr = session.beginTransaction();
        Criteria criteria = session.createCriteria(Sportista.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        Criteria criteria2 = null;
        if (sportista != null) {
            if (!sportista.isEmpty()) {
                criteria.add(Restrictions.ilike("name", sportista, MatchMode.ANYWHERE));
            }
        }
        if (zemlja != null) {
            criteria.add(Restrictions.eq("zemlja", zemlja));
        }
        if (disciplina != 0) {//i sport i disciplina izabrani
            if (criteria2 == null)
                criteria2 = criteria.createCriteria("spordisc");
            criteria2.add(Restrictions.eq("id", disciplina));
        }
        if (sport != null && disciplina == 0) {//samo sport izabran
            if (criteria2 == null)
                criteria2 = criteria.createCriteria("spordisc");
            criteria2.add(Restrictions.eq("sport", sport));
        }
        //constraint nad spordisc tabelom da naziv discipline bude ogranicen na onaj dati string
        if (gender != null) {
            criteria.add(Restrictions.eq("gender", gender));
        }
        if (medal) {
            criteria.add(Restrictions.gt("medals", 0));
        }

        sportists = criteria.list();

        tr.commit();
        boolean good = true;
    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }

    return sportists;
}

From source file:helpers.OrgHelper.java

public static List<Rekordi> getAllRecords(boolean pol) {//sve im vadi hib
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tr = null;/* w w  w .j a v a2 s.co  m*/
    List<Rekordi> records = null;
    try {
        String askfor;
        tr = session.beginTransaction();
        if (pol) {//pol==true
            askfor = "female";
        } else {
            askfor = "male";
        }
        Criteria criteria = session.createCriteria(Rekordi.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.add(Restrictions.eq("pol", askfor));
        Criteria criteria2 = criteria.createCriteria("spordisc")
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria2.addOrder(Order.asc("sport"));
        criteria2.addOrder(Order.asc("disciplina"));

        records = criteria.list();

        /*  for (Rekordi rec : records) {
        Hibernate.initialize(rec.getZemlja());
        Hibernate.initialize(rec.getSpordisc());
          }*/
        tr.commit();
    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }

    return records;
}

From source file:helpers.OrgHelper.java

public static boolean checkSpordiscExists(String sport, String disc) {//sve popuni, dobro radi
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tr = null;//from   w ww  .  j  a  va2s .c o m
    boolean res = false;
    Spordisc sd = null;
    try {
        tr = session.beginTransaction();

        Criteria criteria = session.createCriteria(Spordisc.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        if (disc == null) {
            criteria.add(Restrictions.isNull("disciplina"));
        } else if (disc.isEmpty()) {
            criteria.add(Restrictions.isNull("disciplina"));
        } else {
            criteria.add(Restrictions.ilike("disciplina", disc, MatchMode.EXACT));
        }
        criteria.createCriteria("sport").add(Restrictions.ilike("naziv", sport));

        sd = (Spordisc) criteria.uniqueResult();

        tr.commit();
        if (sd != null) {
            res = true;
        }

    } catch (HibernateException e) {
        if (tr != null) {
            tr.rollback();
        }
    } finally {
        session.close();
    }
    return res;
}