在querydsl中选择特定列时出现JsonMappingException

时间:2018-12-12 07:18:53

标签: spring-data-jpa querydsl

我正在使用spring data jpa.i正在尝试使用querydsl实现搜索过滤器功能。如果我在不使用select子句的情况下实现了querydsl查询,我将获得正确的输出。当我尝试使用两个表实现select子句时,我得到了JsonMappingException和HttpMessageNotWritableException。

ServiceClass

public List<Tuple> buttonSearchClick(String sclient_acct_id,String sacct_desc,String slocation,String sinvestigator_name)
    {
        QAccount account=QAccount.account;
        QInvestigator investigator=QInvestigator.investigator;

        JPQLQuery<Tuple> query = new JPAQuery<Tuple>(entityManager);

         query.select(Projections.bean(Account.class,account.sclient_acct_id,account.sacct_desc,account.slocation,
                      Projections.bean(Investigator.class,investigator.sinvestigator_name).as("investigator")))
         .from(account);

         if(sclient_acct_id!=null)
         {
         query.where(account.sclient_acct_id.eq(sclient_acct_id));
         }
         if(sacct_desc!=null)
         {
         query.where(account.sacct_desc.eq(sacct_desc));
         }
         if(slocation!=null)
         {
         query.where(account.slocation.eq(slocation));
         }
         if(sinvestigator_name!=null)
         {
         query.where(investigator.sinvestigator_name.eq(sinvestigator_name));
         }

     return query.fetch();
    }

ControllerClass:

@RequestMapping(value = "/findcolumn", method = RequestMethod.GET)
public List<Tuple> searchOnClick(
        @RequestParam(value = "sclient_acct_id", required = false) String sclient_acct_id,
        @RequestParam(value = "sacct_desc", required = false) String sacct_desc,
        @RequestParam(value = "slocation", required = false) String slocation,
        @RequestParam(value = "sinvestigator_name", required = false) String sinvestigator_name) {

    return testservice.buttonSearchClick(sclient_acct_id, sacct_desc,
            slocation, sinvestigator_name);
}

**Console:**

Hibernate: select account0_.sclient_acct_id as col_0_0_, account0_.sacct_desc as col_1_0_, account0_.slocation as col_2_0_, investigat1_.sinvestigator_name as col_3_0_ from account account0_ cross join investigator investigat1_ where account0_.ninvestigator_id=investigat1_.ninvestigator_id
2018-12-11 17:38:05.697  WARN 5528 --- [nio-8070-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.example.demo.model.Account["investigator"]->com.example.demo.model.Investigator["ninst_id"])]

0 个答案:

没有答案