Example usage for org.hibernate.criterion DetachedCriteria forClass

List of usage examples for org.hibernate.criterion DetachedCriteria forClass

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria forClass.

Prototype

public static DetachedCriteria forClass(Class clazz) 

Source Link

Document

Static builder to create a DetachedCriteria for the given entity, by its Class.

Usage

From source file:com.photon.phresco.eshop.service.EShopService.java

License:Apache License

public List<ProductHBM> searchProducts(final String namePattern) throws EShopException {
    @SuppressWarnings("unchecked")
    List<ProductHBM> productList = (List<ProductHBM>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            DetachedCriteria criteria = DetachedCriteria.forClass(ProductHBM.class)
                    .add(Restrictions.like("productName", "%" + namePattern + "%"))
                    .add(Restrictions.like("productDescription", "%" + namePattern + "%"));

            List<ProductHBM> productHBMs = template.findByCriteria(criteria);
            List<ProductHBM> productHBMList = new ArrayList<ProductHBM>(10);

            for (ProductHBM productHBM : productHBMs) {
                Object[] values = { 1, 2, 3, 4, 5 };
                List<ReviewHBM> reviewHBMs = session.createCriteria(ReviewHBM.class)
                        .add(Restrictions.in("ratings", values))
                        .add(Restrictions.eq("productId", productHBM.getProductId())).list();

                int rating = 0;

                // TODO average rating calculation not working
                if (reviewHBMs != null && reviewHBMs.size() > 0) {
                    ReviewHBM reviewHBM = reviewHBMs.get(0);
                    rating = ServiceUtil.getRating(reviewHBM.getRatings());
                }/*from ww  w . j  a  v a 2 s  .c o m*/

                productHBM.setRating(rating);
                productHBMList.add(productHBM);
            }

            return productHBMList;
        }
    });

    return productList;
}

From source file:com.pkrete.locationservice.admin.dao.locations.LocationsDaoImpl.java

License:Open Source License

/**
 * Returns the library which locationId matches with the given id number.
 * Initializes areas, collections and shelves related to the given library
 * object, so that the library and the objects related to it can be deleted.
 *
 * @param libraryId locationId that is used for searching
 * @param owner the owner of the object/*from www  . jav a  2 s.  co  m*/
 * @return library with the desired locationId or null if matching library
 * doesn't exist
 */
@Override
public Library getLibraryToBeDeleted(int libraryId, Owner owner) {
    DetachedCriteria criteria = DetachedCriteria.forClass(Library.class)
            .add(Restrictions.eq("locationId", libraryId)).add(Restrictions.eq("owner", owner))
            .setFetchMode("areas", FetchMode.JOIN);
    List<Library> list = (List<Library>) getHibernateTemplate().findByCriteria(criteria);
    if (list.isEmpty()) {
        return null;
    }
    Hibernate.initialize(list.get(0).getDescriptions());
    Hibernate.initialize(list.get(0).getNotes());
    Hibernate.initialize(list.get(0).getImage());
    Hibernate.initialize(list.get(0).getMap());
    List<LibraryCollection> collections = list.get(0).getCollections();
    for (LibraryCollection collection : collections) {
        Hibernate.initialize(collection.getShelves());
    }
    return list.get(0);
}

From source file:com.rockagen.gnext.dao.hb.Hibernate4GenericDaoImpl.java

License:Apache License

/**
 * Create a hibernate {@link Criteria} by {@link DetachedCriteria}
 * /* w w w  .  ja v a  2  s. c o m*/
 * @param criterions
 * @param firstResult
 * @param maxResults
 *            negatives meaning no limit...
 * @return Criteria
 */
protected Criteria createCriteria(DetachedCriteria dcriteria, int firstResult, int maxResults) {
    if (dcriteria == null) {
        dcriteria = DetachedCriteria.forClass(getEntityClass());
    }
    Criteria criteria = dcriteria.getExecutableCriteria(super.getSession());
    if (firstResult > 0) {
        criteria.setFirstResult(firstResult);
    }

    if (maxResults > 0) {
        criteria.setMaxResults(maxResults);
    }
    return criteria;
}

From source file:com.rockagen.gnext.qo.QueryObject.java

License:Apache License

/**
 * Generate hibernate {@link DetachedCriteria}
 * /*from  w  ww . jav a2s .com*/
 * @param clazz
 * @see DetachedCriteria
 * @see Restrictions
 */
public DetachedCriteria generateDetachedCriteria(final Class<?> clazz) {
    return DetachedCriteria.forClass(clazz);
}

From source file:com.rz.action.admin.DriverAction.java

public String list() {
    if (pager == null) {
        pager = new Pager();
    }/* w ww . j  a  v  a2s  .com*/
    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Driver.class);
    if (driverId != null) {
        detachedCriteria.add(Restrictions.eq("id", driverId));
    }

    loginAdmin = this.getLoginAdmin();
    if ("admin".equals(loginAdmin.getUsername())) {
        pager = driverService.findByPager(pager, detachedCriteria);
    } else {
        detachedCriteria.add(Restrictions.eq("company", loginAdmin.getCompany()));
        pager = driverService.findByPager(pager, detachedCriteria);
    }
    return LIST;
}

From source file:com.salesmanager.core.service.catalog.impl.db.dao.CategoryDao.java

License:Open Source License

public Collection<Category> findByCategoryIds(Collection<Long> categoryIds) {

    try {//from  ww  w.  j a v  a 2s  .c  o  m

        if (categoryIds == null)
            return null;

        DetachedCriteria crit = DetachedCriteria.forClass(Category.class);
        crit.add(Expression.in("categoryId", categoryIds));
        Collection result = this.getHibernateTemplate().findByCriteria(crit);

        return result;

    } catch (RuntimeException e) {
        log.error("get failed", e);
        throw e;
    }

}

From source file:com.salesmanager.core.service.catalog.impl.db.dao.CategoryDescriptionDao.java

License:Open Source License

public Collection<CategoryDescription> findByCategoryIds(Collection<Long> categoryIds) {

    try {/*from   w w  w .j  av  a2  s.co m*/

        if (categoryIds == null)
            return null;

        DetachedCriteria crit = DetachedCriteria.forClass(CategoryDescription.class);
        crit.add(Expression.in("id.categoryId", categoryIds));
        Collection result = this.getHibernateTemplate().findByCriteria(crit);

        return result;

    } catch (RuntimeException e) {
        log.error("get failed", e);
        throw e;
    }

}

From source file:com.salesmanager.core.service.reference.impl.dao.CoreModuleServiceDao.java

License:Open Source License

public Collection<CoreModuleService> findByServiceTypeAndSubTypeByRegion(int type, int subType, String region) {

    try {//  w w w  .j a  v a 2  s . c  o  m

        List countryList = new ArrayList();
        countryList.add(region);
        countryList.add(Constants.ALLCOUNTRY_ISOCODE);

        DetachedCriteria crit = DetachedCriteria.forClass(CoreModuleService.class);
        crit.add(Expression.in("countryIsoCode2", countryList));
        crit.add(Expression.eq("coreModuleServiceCode", type));
        crit.add(Expression.eq("coreModuleServiceSubtype", subType));
        crit.addOrder(org.hibernate.criterion.Order.desc("coreModuleServicePosition"));
        Collection result = this.getHibernateTemplate().findByCriteria(crit);

        List countrySpecificList = new ArrayList();
        Iterator i = result.iterator();
        while (i.hasNext()) {
            CoreModuleService cms = (CoreModuleService) i.next();
            if (cms.getCountryIsoCode2().equals(region)) {

                countrySpecificList.add(cms);
            }
        }

        if (countrySpecificList.size() > 0) {
            return countrySpecificList;
        } else {
            return result;
        }

    } catch (RuntimeException re) {
        // log.error("get failed", re);
        throw re;
    }
}

From source file:com.salesmanager.core.service.reference.impl.dao.CoreModuleServiceDao.java

License:Open Source License

public Collection<CoreModuleService> findByServiceTypeAndByRegion(int type, String region) {

    try {//  w w  w .  j a  va2  s. c o m

        List countryList = new ArrayList();
        countryList.add(region);
        countryList.add(Constants.ALLCOUNTRY_ISOCODE);

        DetachedCriteria crit = DetachedCriteria.forClass(CoreModuleService.class);
        crit.add(Expression.in("countryIsoCode2", countryList));
        crit.add(Expression.eq("coreModuleServiceCode", type));
        crit.addOrder(org.hibernate.criterion.Order.desc("coreModuleServicePosition"));
        Collection result = this.getHibernateTemplate().findByCriteria(crit);

        List countrySpecificList = new ArrayList();
        Iterator i = result.iterator();
        while (i.hasNext()) {
            CoreModuleService cms = (CoreModuleService) i.next();
            if (cms.getCountryIsoCode2().equals(region)) {

                countrySpecificList.add(cms);
            }
        }

        if (countrySpecificList.size() > 0) {
            return countrySpecificList;
        } else {
            return result;
        }

    } catch (RuntimeException re) {
        throw re;
    }
}

From source file:com.salesmanager.core.service.reference.impl.dao.CoreModuleServiceDao.java

License:Open Source License

public CoreModuleService findByModuleAndRegion(String module, String region) {

    try {//from ww w  . ja  va  2 s . co  m

        List countryList = new ArrayList();
        countryList.add(region);
        countryList.add(Constants.ALLCOUNTRY_ISOCODE);

        DetachedCriteria crit = DetachedCriteria.forClass(CoreModuleService.class);
        crit.add(Expression.in("countryIsoCode2", countryList));
        crit.add(Expression.eq("coreModuleName", module));
        crit.addOrder(org.hibernate.criterion.Order.desc("coreModuleServicePosition"));
        Collection result = this.getHibernateTemplate().findByCriteria(crit);

        List countrySpecificList = new ArrayList();
        Iterator i = result.iterator();
        CoreModuleService tempCms = null;
        while (i.hasNext()) {
            CoreModuleService cms = (CoreModuleService) i.next();
            if (cms.getCountryIsoCode2().equals(region)) {
                return cms;
            }
            if (cms.getCountryIsoCode2().equals(Constants.ALLCOUNTRY_ISOCODE)) {
                tempCms = cms;
            }
        }

        return tempCms;

    } catch (RuntimeException re) {
        throw re;
    }
}