我遇到了这个错误:
10:54:38,330 ERROR SessionFactoryImpl:363 - Error in named query: transactions.auto org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: : near line 1, column 8 [select :fields from com.TransactionsEntity t where t.transactionDate >= :startDate and t.transactionDate <= :endDate order by id]
意外的AST节点::
选择:来自 com.TransactionsEntity t
的字段清理命名查询:
select :fields from com.TransactionsEntity t where t.transactionDate >= :startDate and t.transactionDate <= :endDate order by id
有什么方法可以使用参数来添加我需要选择的列?我看到我不能在声明的选择部分使用“:”。
由于
答案 0 :(得分:0)
Query query = session.createQuery("select t.field1, t.field2 from com.TransactionsEntity t where t.transactionDate >= :startDate and t.transactionDate <= :endDate order by id");
它会返回对象列表。
List<Object[]> returnedList = query.list();
您可以将其迭代为:
for (Object[] row: returnedList) {
System.out.println("Field 1: " + row[0]);
System.out.println("Field 2: " + row[1]);
}