Search in sources :

Example 1 with SortType

use of com.robo4j.db.sql.support.SortType in project robo4j by Robo4J.

the class DefaultRepository method findByFields.

@SuppressWarnings("unchecked")
@Override
public <T> List<T> findByFields(Class<T> clazz, Map<String, Object> map, int limit, SortType sort) {
    EntityManager em = dataSourceContext.getEntityManager(clazz);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(clazz);
    Root<T> rs = cq.from(clazz);
    List<Predicate> predicates = map.entrySet().stream().map(e -> cb.equal(rs.get(e.getKey()), e.getValue())).collect(Collectors.toList());
    CriteriaQuery<T> cq2 = cq.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(getOrderById(cb, rs, sort)).select(rs);
    TypedQuery<T> tq = em.createQuery(cq2);
    //@formatter:off
    return tq.setMaxResults(limit).getResultList();
//@formatter:on
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) List(java.util.List) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) SortType(com.robo4j.db.sql.support.SortType) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) TypedQuery(javax.persistence.TypedQuery) DataSourceContext(com.robo4j.db.sql.support.DataSourceContext) Root(javax.persistence.criteria.Root) EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate)

Aggregations

DataSourceContext (com.robo4j.db.sql.support.DataSourceContext)1 SortType (com.robo4j.db.sql.support.SortType)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 EntityManager (javax.persistence.EntityManager)1 TypedQuery (javax.persistence.TypedQuery)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Order (javax.persistence.criteria.Order)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1