Search in sources :

Example 81 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project nikita-noark5-core by HiOA-ABI.

the class FondsCreatorService method findFondsCreatorByOwnerPaginated.

// All READ operations
@Override
public List<FondsCreator> findFondsCreatorByOwnerPaginated(Integer top, Integer skip) {
    if (top == null || top > maxPageSize) {
        top = maxPageSize;
    }
    if (skip == null) {
        skip = 0;
    }
    String loggedInUser = SecurityContextHolder.getContext().getAuthentication().getName();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<FondsCreator> criteriaQuery = criteriaBuilder.createQuery(FondsCreator.class);
    Root<FondsCreator> from = criteriaQuery.from(FondsCreator.class);
    CriteriaQuery<FondsCreator> select = criteriaQuery.select(from);
    criteriaQuery.where(criteriaBuilder.equal(from.get("ownedBy"), loggedInUser));
    TypedQuery<FondsCreator> typedQuery = entityManager.createQuery(select);
    typedQuery.setFirstResult(skip);
    typedQuery.setMaxResults(maxPageSize);
    return typedQuery.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) FondsCreator(nikita.model.noark5.v4.FondsCreator)

Example 82 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project nikita-noark5-core by HiOA-ABI.

the class FondsService method findFondsByOwnerPaginated.

// All READ operations
public List<Fonds> findFondsByOwnerPaginated(Integer top, Integer skip) {
    if (top == null || top > maxPageSize) {
        top = maxPageSize;
    }
    if (skip == null) {
        skip = 0;
    }
    String loggedInUser = SecurityContextHolder.getContext().getAuthentication().getName();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Fonds> criteriaQuery = criteriaBuilder.createQuery(Fonds.class);
    Root<Fonds> from = criteriaQuery.from(Fonds.class);
    CriteriaQuery<Fonds> select = criteriaQuery.select(from);
    criteriaQuery.where(criteriaBuilder.equal(from.get("ownedBy"), loggedInUser));
    TypedQuery<Fonds> typedQuery = entityManager.createQuery(select);
    typedQuery.setFirstResult(skip);
    typedQuery.setMaxResults(maxPageSize);
    return typedQuery.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Fonds(nikita.model.noark5.v4.Fonds)

Example 83 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder 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 84 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder 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 85 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.

the class JpaCommodityDAO method getSecurities.

/*
     * @see jgnash.engine.dao.CommodityDAO#getSecurities()
     */
@Override
@NotNull
public List<SecurityNode> getSecurities() {
    List<SecurityNode> securityNodeList = Collections.emptyList();
    try {
        Future<List<SecurityNode>> future = executorService.submit(() -> {
            emLock.lock();
            try {
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<SecurityNode> cq = cb.createQuery(SecurityNode.class);
                Root<SecurityNode> root = cq.from(SecurityNode.class);
                cq.select(root);
                TypedQuery<SecurityNode> q = em.createQuery(cq);
                return stripMarkedForRemoval(new ArrayList<>(q.getResultList()));
            } finally {
                emLock.unlock();
            }
        });
        securityNodeList = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return securityNodeList;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SecurityNode(jgnash.engine.SecurityNode) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) NotNull(jgnash.util.NotNull)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2