Example usage for org.hibernate.criterion Restrictions like

List of usage examples for org.hibernate.criterion Restrictions like

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions like.

Prototype

public static SimpleExpression like(String propertyName, Object value) 

Source Link

Document

Apply a "like" constraint to the named property

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   w w w . ja  v a  2  s .  c  o m

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

            return productHBMList;
        }
    });

    return productList;
}

From source file:com.pmo.pmoitserv.Dao.UtilisateurDao.java

public Utilisateur getUserByLogin_Email(String login, String password) {
    Utilisateur user = null;//from www  . j a  va 2s  .c  o  m
    try {
        this.session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        Criteria c = session.createCriteria(Utilisateur.class);
        Criterion email = Restrictions.eq("utilisateurLogin", login);
        Criterion log = Restrictions.like("utilisateurEmail", login);
        LogicalExpression orExp = Restrictions.or(email, log);
        c.add(orExp);
        c.add(Restrictions.eq("utilisateurPassword", password));
        //.add(Restrictions.disjunction().add(Restrictions.eq("utilisateurLogin", login)).add(Restrictions.eq("utilisateurEmail", login)));
        user = (Utilisateur) c.uniqueResult();
        tx.commit();
        session.flush();
        session.close();
        return user;
    } catch (Exception e) {
        e.printStackTrace();
        session.close();
    }
    return user;
}

From source file:com.pojos.CustomerCredentialsUtility.java

public Customer getCustomerByCredentials(String username) {

    Session s = sessionFactory.openSession();
    List<Customercredentials> customercreds = s.createCriteria(Customercredentials.class)
            .add(Restrictions.like("customerLogin", username)).list();
    //System.out.println(customercreds.get(0).getCustomer().getFirstName());
    return customercreds.get(0).getCustomer();
}