Really dynamic JPA CriteriaBuilder

前端 未结 5 1588
盖世英雄少女心
盖世英雄少女心 2020-12-02 05:50

I need to create a \"real\" dynamic JPA CriteriaBuilder. I get an Map with the statements. It looks like:

nam         


        
5条回答
  •  情话喂你
    2020-12-02 06:16

    I have always been thinking that creation of solution like that is like reinventing the bicycle. Here https://github.com/sasa7812/jfbdemo is my solution. It was tested on EclipseLink and Hibernate (EclipseLink in production, we used it in several projects for simple cases). Sometimes you just need a quick solution to make a dataTable with sorting and filtering, nothing fancy. It is able to filter and sort on joined fields, and even on collections. Project contains demo on Primefaces showing the abilities of FilterCriteriaBuilder. In the heart of it you just need this:

     public List loadFilterBuilder(int first, int pageSize, Map sorts, List argumentFilters, Class entityClass) {
        FilterCriteriaBuilder fcb = new FilterCriteriaBuilder<>(getEntityManager(), (Class) entityClass);
        fcb.addFilters(argumentFilters).addOrders(sorts);
        Query q = getEntityManager().createQuery(fcb.getQuery());
        q.setFirstResult(first);
        q.setMaxResults(pageSize);
        return q.getResultList();
    }
    

    to get the results from database.

    I really need someone to tell me that it is usefull and is used somewhere to continue my work on this library.

提交回复
热议问题