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, String value, MatchMode matchMode) 

Source Link

Document

Apply a "like" constraint to the named property using the provided match mode

Usage

From source file:com.lushapp.modules.sys.web.UserController.java

License:Apache License

/**
 * combogrid/*from   www  .j  a v  a2 s  .  c o  m*/
 *
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "combogrid" })
@ResponseBody
public Datagrid<User> combogrid(@RequestParam(value = "ids", required = false) List<Long> ids,
        String loginNameOrName, Integer rows) throws Exception {
    Criterion statusCriterion = Restrictions.eq("status", StatusState.normal.getValue());
    Criterion[] criterions = new Criterion[0];
    criterions = (Criterion[]) ArrayUtils.add(criterions, 0, statusCriterion);
    Criterion criterion = null;
    if (Collections3.isNotEmpty(ids)) {
        //in?
        Criterion inCriterion = Restrictions.in("id", ids);

        if (StringUtils.isNotBlank(loginNameOrName)) {
            Criterion loginNameCriterion = Restrictions.like("loginName", loginNameOrName, MatchMode.ANYWHERE);
            Criterion nameCriterion = Restrictions.like("name", loginNameOrName, MatchMode.ANYWHERE);
            Criterion criterion1 = Restrictions.or(loginNameCriterion, nameCriterion);
            criterion = Restrictions.or(inCriterion, criterion1);
        } else {
            criterion = inCriterion;
        }
        //??
        criterions = (Criterion[]) ArrayUtils.add(criterions, 0, criterion);
    } else {
        if (StringUtils.isNotBlank(loginNameOrName)) {
            Criterion loginNameCriterion = Restrictions.like("loginName", loginNameOrName, MatchMode.ANYWHERE);
            Criterion nameCriterion = Restrictions.like("name", loginNameOrName, MatchMode.ANYWHERE);
            criterion = Restrictions.or(loginNameCriterion, nameCriterion);
            //??
            criterions = (Criterion[]) ArrayUtils.add(criterions, 0, criterion);
        }
    }

    //
    Page<User> p = new Page<User>(rows);//
    p = userManager.findByCriteria(p, criterions);
    Datagrid<User> dg = new Datagrid<User>(p.getTotalCount(), p.getResult());
    return dg;
}

From source file:com.luxsoft.siipap.dao.core.ClienteDaoImpl.java

License:Apache License

public List<Cliente> buscarClientePorClave(final String clave) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            EventList<Cliente> res = GlazedLists.eventList(session.createCriteria(Cliente.class)
                    .setFetchMode("contactos", FetchMode.JOIN).setFetchMode("telefonos", FetchMode.JOIN)
                    .add(Restrictions.like("clave", clave, MatchMode.START).ignoreCase()).setMaxResults(500)
                    .list());//from   w ww  . j  a v a2 s  .c o m
            return new UniqueList<Cliente>(res, GlazedLists.beanPropertyComparator(Cliente.class, "id"));
        }

    });
}

From source file:com.luxsoft.siipap.dao.core.ClienteDaoImpl.java

License:Apache License

public List<Cliente> buscarClientePorNombre(final String nombre) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            EventList<Cliente> res = GlazedLists.eventList(session.createCriteria(Cliente.class)
                    .setFetchMode("contactos", FetchMode.JOIN).setFetchMode("telefonos", FetchMode.JOIN)
                    .add(Restrictions.like("nombre", nombre, MatchMode.ANYWHERE).ignoreCase())
                    .setMaxResults(500).list());
            return new UniqueList<Cliente>(res, GlazedLists.beanPropertyComparator(Cliente.class, "id"));
        }/*from  ww  w . ja  v  a2s.c  o m*/

    });
}

From source file:com.magmafitness.model.address.AddressDAOHibernate.java

@Override
public List<Address> listByName(String name) {
    return this.session.createCriteria(Address.class).add(Restrictions.like("street", name, MatchMode.ANYWHERE))
            .addOrder(Order.asc("street")).list();
}

From source file:com.nerdzcore.giskardWS.database.dao.impl.ChatBotDaoImpl.java

License:Open Source License

@Override
public List<ChatTriggerEntity> findTriggers(String namePattern) {
    try {//from w ww. j  a v  a 2  s.  co  m
        Criteria criteria = getCurrentSession().createCriteria(ChatTriggerEntity.class, "chatTriggerEntity");
        criteria.add(Restrictions.like("triggerName", namePattern, MatchMode.START).ignoreCase());
        return criteria.list();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, Constants.EXCEPTION_TRACE, e);
        return null;
    }
}

From source file:com.painiu.core.dao.hibernate.PhotoDAOHibernate.java

License:Open Source License

static Criteria buildPhotoCriteria(final Session session, User user, String[] tags, boolean taggedAll,
        String text, Relation relation, boolean count) {
    Criteria criteria = session.createCriteria(Photo.class);

    if (user != null) {
        criteria.add(Restrictions.eq("user", user));

        if (relation != null) {
            criteria.add(/*from   w  w w. ja  v a 2 s  . com*/
                    Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", relation, UserTypes.relation()));
        }
    } else {
        criteria.add(
                Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", Relation.NONE, UserTypes.relation()));

        criteria.setFetchMode("user", FetchMode.JOIN);
    }

    //if (user == null && group == null) {
    if (user == null) {
        Disjunction disjState = Restrictions.disjunction();

        disjState.add(Restrictions.eq("state", Photo.State.USER_POPULAR));
        disjState.add(Restrictions.eq("state", Photo.State.USER_COMMENDATORY));
        disjState.add(Restrictions.eq("state", Photo.State.USER_SENIOR));

        criteria.add(disjState);
    }

    //if (album != null) {
    //   criteria.createAlias("albumPhotos", "ap");
    //   criteria.add( Restrictions.eq("ap.album", album) );
    //}

    //if (group != null) {
    //   criteria.createAlias("groupPhotos", "gp");
    //   criteria.add( Restrictions.eq("gp.group", group) );
    //}

    if ((tags != null && tags.length > 0) || text != null) {
        Criteria subCriteria = criteria.createCriteria("photoTags", "tags");

        if (tags != null && tags.length > 0) {
            if (taggedAll) {
                Conjunction conj = Restrictions.conjunction();
                for (int i = 0; i < tags.length; i++) {
                    conj.add(Restrictions.eq("tagName", tags[i]));
                }
                subCriteria.add(conj);
            } else {
                Disjunction disj = Restrictions.disjunction();
                for (int i = 0; i < tags.length; i++) {
                    disj.add(Restrictions.eq("tagName", tags[i]));
                }
                subCriteria.add(disj);
            }
        }

        if (text != null) {
            Disjunction disj = Restrictions.disjunction();

            disj.add(Restrictions.like("title", text, MatchMode.ANYWHERE));
            disj.add(Restrictions.like("description", text, MatchMode.ANYWHERE));
            disj.add(Restrictions.eq("tags.tagName", text));

            criteria.add(disj);
        }
    }

    // TODO order parameters
    if (!count) {
        /*if (album != null) {
           criteria.addOrder(Order.asc("ap.position"));
        } else*/
        /*if (group != null) {
           criteria.addOrder(Order.asc("gp.position"));
        } else {*/
        criteria.addOrder(Order.desc("timestamp"));
        //}
    }
    // distinct ?
    if ((tags != null && tags.length > 1) || text != null) {
        ProjectionList proj = Projections.projectionList();

        proj.add(Projections.property("id")).add(Projections.property("title"))
                .add(Projections.property("width")).add(Projections.property("height"))
                .add(Projections.property("address.host")).add(Projections.property("address.dir"))
                .add(Projections.property("address.filename")).add(Projections.property("address.secret"))
                .add(Projections.property("address.username")).add(Projections.property("address.fileKey"));

        if (user == null) {
            criteria.createAlias("user", "user");
            proj.add(Projections.property("user.id")).add(Projections.property("user.username"))
                    .add(Projections.property("user.nickname")).add(Projections.property("user.buddyIcon.host"))
                    .add(Projections.property("user.buddyIcon.dir"))
                    .add(Projections.property("user.buddyIcon.filename"))
                    .add(Projections.property("user.buddyIcon.username"))
                    .add(Projections.property("user.buddyIcon.fileKey"));
        }

        criteria.setProjection(Projections.distinct(proj));

        criteria.setResultTransformer(new PhotoBeanResultTransformer());
    }

    return criteria;
}