java - 使用自定义 Jpa 存储库返回页面对象

标签 java spring jpa spring-data

我必须使用过滤器实现搜索方法。这是结构:

我的Jpa界面:

public interface FooRepository extends JpaRepository<Foo, Long>, FooRepositoryCustom {
}

我的自定义界面:

public interface FooRepositoryCustom {
    Page<Foo> findByFilter(FooFilter filter, Pageable pageable);
}

我的自定义实现:

public class FooRepositoryImpl implements FooRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Page<Foo> findByFilter(FppFilter filter, Pageable pageable) {

        CriteriaQuery<Foo> criteriaQuery = em.getCriteriaBuilder().createQuery(Foo.class);
        Root<Foo> root = criteriaQuery.from(Foo.class);
        criteriaQuery.select(root);

        List<Predicate> predicates = new ArrayList<Predicate>();

        if (filter.getFooAttr1() != null) {
            predicates.add(em.getCriteriaBuilder().equal(root.get("fooAttr1"), filter.getFooAttr1()));
        }

        if (filter.getOtherFooId() != null) {
            Join<Foo, OtherFoo> join = root.join("otherFoo", JoinType.LEFT);
            predicates.add(em.getCriteriaBuilder().equal(join.get("id"), filter.getOtherFooId()));
        }

        criteriaQuery.where(predicates.toArray(new Predicate[] {}));

        // Order
        List<Order> orderList = new ArrayList<Order>();
        for (org.springframework.data.domain.Sort.Order order : pageable.getSort()) {
            if (order.getDirection().equals(Direction.ASC)) {
                orderList.add(em.getCriteriaBuilder().asc(root.get(order.getProperty())));
            } else {
                orderList.add(em.getCriteriaBuilder().desc(root.get(order.getProperty())));
            }
        }
        criteriaQuery.orderBy(orderList);

        int totalRows = em.createQuery(criteriaQuery).getResultList().size();
        em.createQuery(criteriaQuery).setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        em.createQuery(criteriaQuery).setMaxResults(pageable.getPageSize());
        Page<Foo> page = new PageImpl<Foo>(em.createQuery(criteriaQuery).getResultList(), pageable, totalRows);

        return page;
    }

}

有一种简单的方法可以返回 Page <Foo>没有标准。否则要添加排序和分页吗?

最佳答案

我不知道规范。 这是我的解决方案:

我实现了我的特殊要求:

public class FooSpecifications {

    public static Specification<Foo> withFilter(final FooFilter filter) {
        return new Specification<Foo>() {
            @Override
            public Predicate toPredicate(Root<Foo> root, CriteriaQuery<?> query,
                    CriteriaBuilder builder) {

                List<Predicate> predicates = new ArrayList<Predicate>();

                if (filter.getAttr1() != null) {
                    predicates.add(builder.equal(root.get("attr1"), filter.getAttr1()));
                }

                if (filter.getOtherFooId() != null) {
                    Join<Foo, OtherFoo> join = root.join("otherFoo", JoinType.LEFT);
                    predicates.add(builder.equal(join.get("id"), filter.getOtherFooId()));
                }

                return builder.and(predicates.toArray(new Predicate[] {}));
            }
        };
    }

}

我将方法添加到界面:

public interface FooRepository extends JpaRepository<Foo, Long>, FooRepositoryCustom {

    Page<Foo> findAll(Specification<Foo> specification, Pageable pageable);
}

并在服务中使用:

fooRepo.findAll(FooSpecifications.withFilter(filter), pageable);

关于java - 使用自定义 Jpa 存储库返回页面对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34908856/

相关文章:

java - 如何动态创建 Spring bean 并在新创建的 bean 上设置注释

java - Spring中使用@Query注解的原生查询

java - 在 Spring + Sockjs 应用程序上手动设置传输类型

java - 图片直接从客户端上传到远程服务器? Spring / Tomcat

java - 正确的用户输入与数组值不匹配

java - 从 CLOB(Oracle) 中的记录构造组合 json

jpa - JPQL 更新查询执行更新时事务需要异常

java - 如何使用嵌入式 id 编写 JPQL SELECT?

java - 无法部署分解的 Artifact

Java 外部进程错误流 readLine() 间歇性阻塞