use of javax.persistence.criteria.Predicate in project tomee by apache.
the class MoviesBean method getMovies.
public List<Movie> getMovies(Integer firstResult, Integer maxResults, String field, String searchTerm) {
CriteriaBuilder qb = entityManager.getCriteriaBuilder();
CriteriaQuery<Movie> cq = qb.createQuery(Movie.class);
Root<Movie> root = cq.from(Movie.class);
EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);
if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
Path<String> path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%");
cq.where(condition);
}
TypedQuery<Movie> q = entityManager.createQuery(cq);
if (maxResults != null) {
q.setMaxResults(maxResults);
}
if (firstResult != null) {
q.setFirstResult(firstResult);
}
return q.getResultList();
}
use of javax.persistence.criteria.Predicate in project tomee by apache.
the class MoviesBean method count.
public int count(String field, String searchTerm) {
CriteriaBuilder qb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = qb.createQuery(Long.class);
Root<Movie> root = cq.from(Movie.class);
EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);
Path<String> path = root.get(type.getDeclaredSingularAttribute(field, String.class));
Predicate condition = qb.like(path, "%" + searchTerm + "%");
cq.select(qb.count(root));
cq.where(condition);
return entityManager.createQuery(cq).getSingleResult().intValue();
}
use of javax.persistence.criteria.Predicate in project che by eclipse.
the class JpaRecipeDao method search.
@Override
@Transactional
public List<RecipeImpl> search(String user, List<String> tags, String type, int skipCount, int maxItems) throws ServerException {
try {
final EntityManager manager = managerProvider.get();
final CriteriaBuilder cb = manager.getCriteriaBuilder();
final CriteriaQuery<RecipeImpl> query = cb.createQuery(RecipeImpl.class);
final Root<RecipeImpl> fromRecipe = query.from(RecipeImpl.class);
final ParameterExpression<String> typeParam = cb.parameter(String.class, "recipeType");
final Predicate checkType = cb.or(cb.isNull(typeParam), cb.equal(fromRecipe.get("type"), typeParam));
final TypedQuery<RecipeImpl> typedQuery;
if (tags != null && !tags.isEmpty()) {
final Join<RecipeImpl, String> tag = fromRecipe.join("tags");
query.select(cb.construct(RecipeImpl.class, tag.getParent())).where(cb.and(checkType, tag.in(tags))).groupBy(fromRecipe.get("id")).having(cb.equal(cb.count(tag), tags.size()));
typedQuery = manager.createQuery(query).setParameter("tags", tags);
} else {
typedQuery = manager.createQuery(query.where(checkType));
}
return typedQuery.setParameter("recipeType", type).setFirstResult(skipCount).setMaxResults(maxItems).getResultList();
} catch (RuntimeException ex) {
throw new ServerException(ex.getLocalizedMessage(), ex);
}
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class IdClassPredicateTest method testDeclaredIdClassAttributes.
@Test
public void testDeclaredIdClassAttributes() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
// Packaging arguments for use in query.
List<String> divisions = new ArrayList<String>();
divisions.add("NA");
divisions.add("EU");
// Building the query.
CriteriaBuilder criteria = em.getCriteriaBuilder();
CriteriaQuery<Widget> query = criteria.createQuery(Widget.class);
Root<Widget> root = query.from(Widget.class);
Predicate predicate = root.get("division").in(divisions);
query.where(predicate);
// Retrieving query.;
List<Widget> widgets = em.createQuery(query).getResultList();
Assert.assertEquals(4, widgets.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class IdClassPredicateTest method testSupertypeIdClassAttributes.
@Test
public void testSupertypeIdClassAttributes() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
// Packaging arguments for use in query.
List<String> types = new ArrayList<String>();
types.add("NA");
types.add("EU");
// Building the query.
CriteriaBuilder criteria = em.getCriteriaBuilder();
CriteriaQuery<Tool> query = criteria.createQuery(Tool.class);
Root<Tool> root = query.from(Tool.class);
Predicate predicate = root.get("type").in(types);
query.where(predicate);
// Retrieving query.
List<Tool> tools = em.createQuery(query).getResultList();
Assert.assertEquals(4, tools.size());
em.getTransaction().commit();
em.close();
}
Aggregations