1

我无法SELECT DISTINCT taxtCode从此代码中添加:

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 cannot be cast to com.test.test.TblTaxType出现错误。此查询如何返回值列表?每当添加单词 DISTINCT 时都会发生错误。在 HQL 中是否不可能像在 SQL Query 中那样使用 distinct 并返回值列表?

4

4 回答 4

1

解决了它,GROUP BY而不是使用DISTINCT.

String hql = "FROM TblTaxType tbl_tax_type WHERE bfnsCode = ? GROUP BY taxtCode";
于 2013-08-26T09:53:21.190 回答
1

您还可以同时使用 Criteria 和 Projection :

Criteria criteria = session.createCriteria(MyEntity.class);
criteria.setProjection(Projections.distinct(Projections.property( "id" )));

希望它可以帮助某人。

于 2013-08-26T06:20:41.137 回答
0

When you start your query with FROM TblTaxType OR Select * it returns the table rows with all your table Columns and assigns it to the list with List <TblTaxType> DataType hence it doesn't give any error.

But when you write Select colName it returns only the string present in that table cell. Which it can't convert to java.lang.String cannot be cast to com.test.test.classes.TblTaxType

There is no Problem in Using Distinct in Hibernate. See here :- How do you create a Distinct query in HQL

于 2013-08-25T11:38:23.127 回答
0

使用hibernate执行此操作的正确方法是

select tbl_tax_type  FROM TblTaxType tbl_tax_type WHERE BFNS_CODE = ?

编辑 2

获取不同列使用的特定列表:

select distinct(taxtCode) from TblTaxType WHERE BFNS_CODE = ?

在休眠中,您不必使用第一个 select 语句来获取列表,只需使用 from 语句,如下所示:

from TblTaxType where BFNS_CODE = ?
于 2013-08-25T12:20:56.783 回答