Announcement Announcement Module
Collapse
No announcement yet.
Custom implementation and Sorting issues Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom implementation and Sorting issues

    Hi

    I have JpaAdvertiseCacheRepositoryImpl which is a custom repository implementation of SpringDataJpa. I need this for a complicated criteria.

    I managed to do filter criterias via Predicates and pagination via TypedQuery.

    My big issue now is Sorting! Could not find a way to do this in custom implementation of repo.

    Anybody knows how to do that?

    Tnx

    CODE:
    Code:
    public class JpaAdvertiseCacheRepositoryImpl implements JpaAdvertiseCacheRepositoryCustom {
    
        @PersistenceContext
        private EntityManager em;
    
        @Override
        public List<AdvertiseCacheJpa> findByFilter(FilterBuilder filter) {
    
            CriteriaBuilder builder = em.getCriteriaBuilder();
            CriteriaQuery<AdvertiseCacheJpa> query = builder.createQuery(AdvertiseCacheJpa.class);
            Root<AdvertiseCacheJpa> from = query.from(AdvertiseCacheJpa.class);
    
            //apply filter <- applyFilter returns array of predicates
            Predicate[] predicates = applyFilter(filter, builder, from);
    
            if (predicates.length > 0) {
                //query.where(builder.and(predicates));
                query.where(predicates);
            }
    
           //apply sorting <- THIS HAVE TO WORK SOON :)
            Sort sort = new Sort(Sort.Direction.ASC, "price");
           //query.orderBy()
    
           //apply pagination
           TypedQuery typedQuery = em.createQuery(query);
           typedQuery = typedQuery.setFirstResult(filter.getOffset());
           typedQuery = typedQuery.setMaxResults(filter.getMaxItemsPerPage());
           return typedQuery.getResultList();
          //return  em.createQuery(query).getResultList();
        }
Working...
X