想要将Json对象映射到JPA规范 [英] want to map Json object to JPA specification

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

问题描述

我有一个RequestMaping,将一些json类作为主体参数。
我想从json对象创建适当的规范,以便传递给我的Repository,如下所示:

  pagesResult = myRepository。 findAll(mySpec)

我有解析和动态添加项目到规范。我想要这样做:

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

列表< PhoneSpecification> specificationlist = new ArrayList();

generateSpecifications(searchObject,specificationslist); // fill specificationList

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

//这是我的问题,我不得不动态增加我的规格如下:
specificationOfPhone = specificationOfPhone +规格。而(SPEC);

}

mobileRepository.findAll(specificationOfPhone);


解决方案

我终于通过添加第一个规范和通过这样添加来处理全部:

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

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


I have a RequestMaping that get some json class as body params. I want to create proper Specification from 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天全站免登陆