如何将Json对象映射到JPA规范以进行搜索查询 [英] How to map Json object to JPA specification for Search query

查看:296
本文介绍了如何将Json对象映射到JPA规范以进行搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RequestMaping,它获取Search json类作为主体参数. 我想从此搜索json对象创建适当的规范,以便像这样传递到我的存储库:

I have a RequestMaping that get a Search json class as body params. I want to create proper Specification from this search json object so that pass to my Repository like this:

pagesResult = myRepository.findAll(mySpec)

我对解析有疑问,并在规范中动态添加项目.我想实现这样的目标:

I have problme with parsing and Dynamically add items to specification. I want to achieve something like this:

   @Override
    public Phones searchPhones(int pageNumber, Map<String, String> searchObject) {

        List<PhoneSpecification> specificationslist = new ArrayList<>();

        generateSpecifications(searchObject, specificationslist); //fill specificationList

        Specification<Phone> specificationOfPhone;
        for (PhoneSpecification spec :
                specificationslist) {

           //this is my problem , I had to dynamically increase my Specification like this:
            specificationOfPhone = specificationOfPhone + Specifications.and(spec);

        }

mobileRepository.findAll(specificationOfPhone);

推荐答案

我最终通过在第一个规范中添加位置并通过添加以下内容来处理所有问题来实现这一点:

I finally achieve this by adding where to first specification and handle all by adding like this:

if(specificationslist.size()>0){
            finalSpecification = Specifications.where(specificationslist.get(0)) ;
        }

        for (int i=1 ; i<specificationslist.size(); i++) {
            finalSpecification = Specifications.where(finalSpecification).and(specificationslist.get(i));
        }

这篇关于如何将Json对象映射到JPA规范以进行搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆