java - Hibernate: Unexpected Error while using Named Query -
i getting error when trying display few contents of table using named query in hibernate. have tried looking answers, no success. code listed below.
<sql-query name="activecustomers"> <return alias="cts" class="customer"/> select cts.cid {cts.cid}, cts.cname {cts.cname}, cts.email {cts.email}, cts.status {cts.status} customers cts cts.status=:st </sql-query>
from client side invoking shown below:
sessionfactory sf=chibernateutil.getsessionfactory(); session session=sf.opensession(); tx=session.begintransaction(); list=session.getnamedquery("activecustomers").setstring("st","active").list(); for(customer c:list){ system.out.println(c); } tx.commit(); session.close();
but getting error:
hibernate:
select cts.cid cid0_, cts.cname cname0_0_, cts.email email0_0_, cts.status status0_0_ customers cts cts.status=? org.hibernate.exception.genericjdbcexception: not execute query @ org.hibernate.exception.errorcodeconverter.handlednonspecificexception(errorcodeconverter.java:92) . . caused by: java.sql.sqlexception: column 'city0_0_' not found.
but if add columns of table works. that's not requirement. requirement display cid,cname,email , status.
finally figured out.
when using below syntax need specify entire class variables , return type object type or class type.
<sql-query name="activecustomers"> <return alias="cts" class="customer"/> select cts.cid {cts.cid}, cts.cname {cts.cname}, cts.email {cts.email}, cts.status {cts.status} customers cts cts.status=:st </sql-query>
on other hand when want select few or specific columns need use different tag called
<return scalar ...
here return type object array , object array need print out values. meet our requirement above query can re-written follows:
also note difference in syntax of select statements.
<sql-query name="activecustomers"> <return-scalar column="cid" type="string"/> <return-scalar column="cname" type="string"/> <return-scalar column="email" type="string"/> <return-scalar column="status" type="string"/> select cts.cid cid, cts.cname cname, cts.email email, cts.status status customers cts cts.status=:st </sql-query>
the change in client side how read data ..
for(object obj[]:aclist){ for(object o:obj){ system.out.println(o); } }
Comments
Post a Comment