Hibernate QuerySyntaxException构建动态查询传递所有参数

时间:2017-10-19 21:34:14

标签: java hibernate jpa criteria-api

我有以下示例JSON,它将从UI生成:

{
"ActionType":"LOOKUP",
"Action": {
"TableName": "ProductTable",
"Select": ["ProductName", "ProductType"],
"Where": ["productType"],
"Values": ["10.00"]
"OPERATOR":""
}
}

当我反序列化JSON并尝试使用以下代码构建动态查询时,它失败了:nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: ProductTable is not mapped

List<String> results = new ArrayList<String>();

for (Lookup lookup: lookups){           
    StringBuilder builder = new StringBuilder("select " + StringUtils.join(lookup.getFieldsToSelect(), ",") + " from " + lookup.getTableName());            
    if (!lookup.getWhere().isEmpty()) {
        for (Where where : lookup.getWhere()) {
            if (where.getKey() != null && where.getValue() != null){
                builder.append(" where " + where.getKey() + "=" + where.getValue());
            }

            if (where.getOperator().equals("AND") || where.getOperator().equals("OR")){
                builder.append(" " + where.getOperator());
            }
        }
    }           
    Query query = em.createQuery(builder.toString());       
    for (Object o : query.getResultList()) {
        Object[] cols = (Object[]) o;
        results.add((String)cols[0]);
    }
}
return results;

如果我使用上述方法构建动态查询,是否需要使用指定表的@Entity类?

0 个答案:

没有答案