Search in sources :

Example 16 with Predicate

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate)

Example 17 with Predicate

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate)

Example 18 with Predicate

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);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ServerException(org.eclipse.che.api.core.ServerException) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Predicate(javax.persistence.criteria.Predicate) Transactional(com.google.inject.persist.Transactional)

Example 19 with Predicate

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 20 with Predicate

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

Predicate (javax.persistence.criteria.Predicate)35 EntityManager (javax.persistence.EntityManager)19 Test (org.junit.Test)16 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)15 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)7 DAOException (org.jbei.ice.storage.DAOException)7 ArrayList (java.util.ArrayList)4 TypedQuery (javax.persistence.TypedQuery)3 List (java.util.List)2 Map (java.util.Map)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 TestForIssue (org.hibernate.testing.TestForIssue)2 SampleRequest (org.jbei.ice.lib.dto.sample.SampleRequest)2 Request (org.jbei.ice.storage.model.Request)2 Function (com.google.common.base.Function)1 Transactional (com.google.inject.persist.Transactional)1 DataSourceContext (com.robo4j.db.sql.support.DataSourceContext)1 SortType (com.robo4j.db.sql.support.SortType)1