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:com.lyh.licenseworkflow.dao.EnhancedHibernateDaoSupport.java

/**
 * ?? from DeviceResource where type = 'ROUTER' and temp = false;
 *
 * @param propertyNames ???// ww w. j a v  a  2 s.  c o  m
 * @param values        
 * @return ??
 */
@SuppressWarnings("unchecked")
public List<T> getEntitiesByPropNames(final String[] propertyNames, final Object[] values) {
    if (ArrayUtils.isEmpty(propertyNames) || ArrayUtils.isEmpty(values)
            || propertyNames.length != values.length) {
        throw new IllegalArgumentException("arguments is invalid.");
    }
    return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {

        @Override
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria c = session.createCriteria(getEntityName());
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                if (propertyName.indexOf(".") != -1) {
                    Criteria subC = null;
                    String[] props = StringUtils.split(propertyName, ".");
                    for (int j = 0; j < props.length; j++) {
                        if (j != props.length - 1) {
                            subC = c.createCriteria(props[j]);
                        }
                    }
                    if (values[i] == null)
                        subC.add(Restrictions.isNull(propertyName.substring(propertyName.indexOf(".") + 1)));
                    else
                        subC.add(Restrictions.eq(propertyName.substring(propertyName.indexOf(".") + 1),
                                values[i]));
                } else {
                    if (values[i] == null)
                        c.add(Restrictions.isNull(propertyNames[i]));
                    else if (values[i].toString().indexOf("%") != -1) {// ,?value?%
                        c.add(Restrictions.like(propertyNames[i], values[i]));
                    } else {
                        c.add(Restrictions.eq(propertyNames[i], values[i]));
                    }
                }
            }
            return c.list();
        }
    });
}

From source file:com.lyh.licenseworkflow.dao.EnhancedHibernateDaoSupport.java

/**
 * ??? from DeviceResource where type = 'ROUTER' and temp = false;
 *
 * @param propertyNames ???//from ww  w .  j a v  a  2  s .co  m
 * @param values        
 * @return ??
 */
@SuppressWarnings("unchecked")
public int countEntitiesByPropNames(final String[] propertyNames, final Object[] values) {
    if (ArrayUtils.isEmpty(propertyNames) || ArrayUtils.isEmpty(values)
            || propertyNames.length != values.length) {
        throw new IllegalArgumentException("arguments is invalid.");
    }
    return (Integer) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria c = session.createCriteria(getEntityName());
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                if (propertyName.indexOf(".") != -1) {
                    Criteria subC = null;
                    String[] props = StringUtils.split(propertyName, ".");
                    for (int j = 0; j < props.length; j++) {
                        if (j != props.length - 1) {
                            subC = c.createCriteria(props[j]);
                        }
                    }
                    if (values[i] == null)
                        subC.add(Restrictions.isNull(propertyName.substring(propertyName.indexOf(".") + 1)));
                    else
                        subC.add(Restrictions.eq(propertyName.substring(propertyName.indexOf(".") + 1),
                                values[i]));
                } else {
                    if (values[i] == null)
                        c.add(Restrictions.isNull(propertyNames[i]));
                    else
                        c.add(Restrictions.eq(propertyNames[i], values[i]));
                }
            }
            int num = ((Integer) c.setProjection(Projections.rowCount()).uniqueResult()).intValue();// ???
            return new Integer(num);
        }
    });
}

From source file:com.lyh.licenseworkflow.dao.EnhancedHibernateDaoSupport.java

/**
 * ?? from DeviceResource where type = 'ROUTER' and temp = false;
 *
 * @param start         ?//  w  w w.j a  va 2  s. c  o m
 * @param pageSize      ??
 * @param propertyNames ???
 * @param values        
 * @return ??
 */
@SuppressWarnings("unchecked")
public List<T> getPagingEntitiesByPropNames(final int start, final int pageSize, final String[] propertyNames,
        final Object[] values) {
    if (ArrayUtils.isEmpty(propertyNames) || ArrayUtils.isEmpty(values)
            || propertyNames.length != values.length) {
        throw new IllegalArgumentException("arguments is invalid.");
    }
    return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria c = session.createCriteria(getEntityName());
            c.setFirstResult(start);
            c.setMaxResults(pageSize);
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                if (propertyName.indexOf(".") != -1) {
                    Criteria subC = null;
                    String[] props = StringUtils.split(propertyName, ".");
                    for (int j = 0; j < props.length; j++) {
                        if (j != props.length - 1) {
                            subC = c.createCriteria(props[j]);
                        }
                    }
                    if (values[i] == null)
                        subC.add(Restrictions.isNull(propertyName.substring(propertyName.indexOf(".") + 1)));
                    else
                        subC.add(Restrictions.eq(propertyName.substring(propertyName.indexOf(".") + 1),
                                values[i]));
                } else {
                    if (values[i] == null)
                        c.add(Restrictions.isNull(propertyNames[i]));
                    else if (values[i].toString().indexOf("%") != -1) {// ,?value?%
                        c.add(Restrictions.like(propertyNames[i], values[i]));
                    } else {
                        c.add(Restrictions.eq(propertyNames[i], values[i]));
                    }
                }
            }
            return c.list();
        }
    });
}

From source file:com.marc.lastweek.business.entities.classifiedad.repository.ClassifiedAdRepository.java

License:Open Source License

private Criteria advancedSearchQueryConstructor(FilterParameters parameters, Calendar from) {

    Criteria criteriaQuery = this.sessionFactory.getCurrentSession().createCriteria(ClassifiedAd.class);

    if (parameters.getCategoryId() != null) {
        criteriaQuery.createCriteria("category").add(Restrictions.eq("id", parameters.getCategoryId()));
    }// ww  w .jav a 2s . c o  m
    if (parameters.getProvinceId() != null) {
        criteriaQuery.createCriteria("province").add(Restrictions.eq("id", parameters.getProvinceId()));
    }
    if (parameters.getSubcategoryId() != null) {
        criteriaQuery.createCriteria("subcategory").add(Restrictions.eq("id", parameters.getSubcategoryId()));
    }
    criteriaQuery.add(Restrictions.ge("publicationDate", from));
    criteriaQuery.add(Restrictions.eq("state", Integer.valueOf(ClassifiedAd.STATE_ACTIVE)));
    return criteriaQuery;
}

From source file:com.medigy.service.impl.insurance.InsurancePolicyFacadeImpl.java

License:Open Source License

public List listInsurancePlans(final InsuranceProduct product) {
    Criteria criteria = getSession().createCriteria(InsurancePlan.class);
    criteria.createCriteria("insuranceProduct")
            .add(Restrictions.eq("insuranceProductId", product.getInsuranceProductId()));
    return criteria.list();
}

From source file:com.mercatis.lighthouse3.persistence.environment.hibernate.DeploymentCarryingDomainModelEntityDAOImplementation.java

License:Apache License

@Override
protected Criteria entityToCriteria(Session session, Entity entityTemplate) {

    Criteria criteria = super.entityToCriteria(session, entityTemplate);

    if (!entityTemplate.getDeployments().isEmpty()) {
        Criteria deploymentCriteria = criteria.createCriteria("attachedDeployments");

        for (Deployment deployment : entityTemplate.getAllDeployments()) {
            deploymentCriteria.add(Restrictions.eq("id", deployment.getId()));
        }//w  w w  .  j a  v  a2  s.com
    }

    return criteria;
}

From source file:com.mil.randommenu.dao.MenuItemDao.java

public List<MenuItem> getMenuItemFromWeekMenus() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MenuItem.class);
    Criteria menuCriteria = criteria.createCriteria("menu");
    menuCriteria.add(Restrictions.eq("isWeekMenu", true));
    return criteria.list();
}

From source file:com.mil.randommenu.dao.MenuItemDao.java

public List<Vegetable> getVegdetablesFromWeekMenus() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MenuItem.class);
    Criteria menuCriteria = criteria.createCriteria("menu");
    menuCriteria.add(Restrictions.eq("isWeekMenu", true));
    criteria.setProjection(Projections.property("vegetable"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.mil.randommenu.dao.MenuItemDao.java

public Long countVegetableFromWeekMenus(Vegetable vegetable) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MenuItem.class);
    Criteria menuCriteria = criteria.createCriteria("menu");
    menuCriteria.add(Restrictions.eq("isWeekMenu", true));
    Criteria vegetableCriteria = criteria.createCriteria("vegetable");
    vegetableCriteria.add(Restrictions.eq("name", vegetable.getName()));
    criteria.setProjection(Projections.sum("quantity"));
    return (Long) criteria.uniqueResult();
    //        return (Long) criteria.list();
}

From source file:com.mx.teknei.pc.lib.dao.imp.RecpNavDAO.java

@Override
public SfmoReceNave findByIdVehi(int idVehi) {
    SfmoReceNave recpnav = null;//from  www  . j  av a2  s . c om
    Transaction trans = null;
    Session session = getSessionFactory().openSession();
    try {
        trans = session.beginTransaction();
        Criteria crit = session.createCriteria(SfmoReceNave.class);
        Criteria vehi = crit.createCriteria("sfvhVehi");
        vehi.add(Restrictions.eq("idVehi", idVehi));
        recpnav = (SfmoReceNave) crit.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        trans.commit();
        session.flush();
        session.close();
    }
    return recpnav;
}