我无法从此代码添加SELECT DISTINCTtaxtCode
:
public List<TblTaxType> findAlltaxtCode(String bfnsCode) {
List<TblTaxType> result = null;
String hql = "select distinct(taxtCode) from TblTaxType tbl_tax_type WHERE bfnsCode = ?";
try {
setSession(HibernateUtil.getSession());
@SuppressWarnings("unchecked")
List <TblTaxType> resultList = getSession().createQuery(hql)
.setString(0, bfnsCode)
.list();
if(!resultList.isEmpty()){
result = resultList;
Debugger.print("TAX CODES FOUND ");
}else{
Debugger.print("TAX CODES NOT FOUND ");
}
} catch (Exception e) {
e.printStackTrace();
Debugger.print(" TAX CODES NOT FOUND ");
}
Debugger.print(hql);
closeSession();
return result;
}
更新为整个代码。查询是正确的,但似乎它没有返回列表值。仍然出现 java.lang.String 无法转换为 com.test.test.TblTaxType 错误。这个查询如何返回一个值列表?每当添加单词 DISTINCT 时就会发生错误。在 HQL 中是否不可能像 SQL 查询那样使用不同值并返回值列表?
最佳答案
使用GROUP BY
解决它,而不是使用DISTINCT
。
String hql = "FROM TblTaxType tbl_tax_type WHERE bfnsCode = ? GROUP BY taxtCode";
关于java - 在 createQuery 中使用 SELECT DISTINCT 返回列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18428348/