我有一个使用表格的SQL查询。结果集返回到bean类,该类未映射到数据库中的表。代码在这里:
SQLQuery q2=ss.createSQLQuery("select tbl_policy.policyNum as POLICYNUM FROM tbl_policy join tbl_product on tbl_policy.FK_productId = tbl_product.pk_product_id join tbl_code on tbl_policy.FK_codeId = tbl_code.PK_codeId join tbl_agriyear on tbl_policy.FK_agriYearId = tbl_agriyear.pk_agriyear_id where tbl_policy.FK_naturalInsurantId = :p1 and tbl_agriyear.AGRIYEAR =:p2");
q2.addScalar("POLICYNUM", Hibernate.STRING);
List<SearchPolicyBean> lsql = (List<SearchPolicyBean>)q2.list();
bean类名是:SearchPolicyBean 当我运行它时,在这一行
System.out.println("Finalllll "+lsql.get(0).getPOLICYNUM());
出现此错误:
java.lang.String cannot be cast to BO.SearchPolicyBean
答案 0 :(得分:0)
addScalar(String columnAlias)
Is to Declare a scalar query result.
您在此处将其声明为字符串q2.addScalar("POLICYNUM", Hibernate.STRING);
很明显,查询会产生一个String列表而不是SearchPolicyBean pojos。
如果您要查看您的pojo classe列表,则必须使用
q2.addEntity(SearchPolicyBean.Class);
并根据
更改您的查询答案 1 :(得分:0)
获取第一个元素:
setPolicyNum((String)((Object [])polList.get(i))[0]);