Monday, 15 June 2015

Cast exception in native sql query using hibernate -



Cast exception in native sql query using hibernate -

i have sql query using tables in it. result set homecoming bean class not mapped table in database. code here:

sqlquery q2=ss.createsqlquery("select tbl_policy.policynum policynum tbl_policy bring together tbl_product on tbl_policy.fk_productid = tbl_product.pk_product_id bring together tbl_code on tbl_policy.fk_codeid = tbl_code.pk_codeid bring together tbl_agriyear on tbl_policy.fk_agriyearid = tbl_agriyear.pk_agriyear_id tbl_policy.fk_naturalinsurantid = :p1 , tbl_agriyear.agriyear =:p2"); q2.addscalar("policynum", hibernate.string); list<searchpolicybean> lsql = (list<searchpolicybean>)q2.list();

bean class name is: searchpolicybean when run it, in line

system.out.println("finalllll "+lsql.get(0).getpolicynum());

this error appears:

java.lang.string cannot cast bo.searchpolicybean

addscalar(string columnalias) declare scalar query result.

you declared string here q2.addscalar("policynum", hibernate.string);

so query results string list not searchpolicybean pojos .

if expectiong list of pojo classe have use

q2.addentity(searchpolicybean.class);

and alter query according.

sql hibernate java-ee

No comments:

Post a Comment