Querydsl:org.springframework.http.converter.HttpMessageNotWritableException和com.fasterxml.jackson.databind.JsonMappingException:

时间:2018-03-08 15:20:49

标签: json spring jackson querydsl many-to-one

我正在使用Querydsl(4.1.1)来实现搜索过滤器。我想知道如何在Querydsl中选择特定列。 我尝试使用Querydsl select子句选择特定列。但我得到了例外。

的AccountController

public List<String> seach() {

        return accService.Search();
    }

AccountSercice

public List<String> Search() {  
    QAccount account = QAccount.account;
    JPAQuery query = new JPAQuery(em);
             query.select(account.sclientacctid, account.sacctdesc).from(account);
    return query.fetch();
}

异常

org.springframework.http.converter.HttpMessageNotWritableException
com.fasterxml.jackson.databind.JsonMappingException:
java.lang.ClassCastException: com.querydsl.core.types.QTuple$TupleImpl cannot be cast to java.lang.String

1 个答案:

答案 0 :(得分:0)

您可以使用选择过滤掉列:

JPAQuery query = new JPAQuery(em);
QAccountModel accountModel = QAccountModel.accountModel;
List<Tuple> result = query.select(accountModel.sacctdesc).from(accountModel).fetch();
for (Tuple row : result) {
 System.out.println("sacctdesc " + row.get(accountModel.sacctdesc));
}

或通过原生查询:

//example with native query
HibernateSQLQuery query = new HibernateSQLQuery(session, templates);
List<String> names = query.from(cat).list(cat.name);