lundi 9 mars 2015

How to right convert from Expression to CriteriaBuilder?



Sorry my English. I have code with Expression



Criteria criteria = getSession().createCriteria(TagSynonym.class);
criteria.add(Expression.like("title", "%"+piece+"%") );
criteria.setFirstResult(0);
criteria.setMaxResults(maxTagsInResult);
return criteria.list();


But the expression is outdated? And to do with the CriteriaBuilder?



EntityManager em = this.getEntityManager();
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<TagSynonym> query = criteriaBuilder.createQuery(TagSynonym.class);

Root<TagSynonym> fromTagsSynonyms = query.from(TagSynonym.class);
Predicate likesTags = criteriaBuilder.equal(fromTagsSynonyms.get(TagSynonym_.title), "%"+piece+"%");
query.select(fromTagsSynonyms).where(likesTags);
return em.createQuery(query).getResultList();


But the code does not work ... (returns an empty array) + I have not figured out how to make the Limit




Aucun commentaire:

Enregistrer un commentaire