Wednesday, 15 July 2015

hibernate - selecting list of object using HQL when the object is a property of another entity class -



hibernate - selecting list of object using HQL when the object is a property of another entity class -

i trying list<role> next entity class using hql-

@entity @table(name="appl_approval_roles") public class applapprovalroles implements serializable, comparable<applapprovalroles> { private static final long serialversionuid = 1l; @id @column(name="id", updatable=false) @sequencegenerator(name="appl_approval_roles_id_seq",sequencename="appl_approval_roles_id_seq") @generatedvalue(generator="appl_approval_roles_id_seq") private int id; @column(name="appl_name") private string applname; @onetoone @joincolumn(name="client_id") private client client; @onetoone @joincolumn(name="role_id") private role role; @column(name="stage_num") private int stagenum; @column(name="active",length=1) @type(type="yes_no") private boolean active = false; public applapprovalroles(){} public applapprovalroles(int stagenum, string applname, client client, role role, boolean active){ this.stagenum = stagenum; this.applname = applname; this.client = client; this.role = role; this.active = active; } public int getid() { homecoming id; } public void setid(int id) { this.id = id; } public string getapplname() { homecoming applname; } public void setapplname(string applname) { this.applname = applname; } public client getclient() { homecoming client; } public void setclient(client client) { this.client = client; } public role getrole() { homecoming role; } public void setrole(role role) { this.role = role; } public int getstagenum() { homecoming stagenum; } public void setstagenum(int stagenum) { this.stagenum = stagenum; } public boolean isactive() { homecoming active; } public void setactive(boolean active) { this.active = active; } }

the role class -

@entity @table(name="role") public class role implements serializable, comparable<role> { @id @column(name="id", updatable=false) @sequencegenerator(name="role_id_seq", sequencename="role_id_seq") @generatedvalue(generator="role_id_seq") private int id; @onetoone @joincolumn(name="client_id", nullable=false) private client client; @column(name="code", nullable=true) private string code; @column(name="name", nullable=false) private string name; @column(name="binary_code") private string binarycode; public role(){} public role(int id, string code, string name) { this.id = id; this.code = code; this.name = name; } public role(string code, string name, client client) { this.code = code; this.name = name; this.client = client; } public role(client client) { this.client = client; } public int getid() { homecoming id; } public void setid(int id) { this.id=id; } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } public string getcode() { homecoming code; } public void setcode(string code) { this.code = code; } public client getclient() { homecoming client; } public void setclient(client client) { this.client = client; } }

the hql trying -

@suppresswarnings("unchecked") public list<role> getrolebyclientandappname(client client, string applname){ string hql = "select role applapprovalroles client_id = :clientid , appl_name = :applname"; session session = this.getsessionfactory().getcurrentsession(); query query = session.createquery(hql); query.setparameter("clientid", client.getid()); query.setparameter("applname", applname); list<role> roles = (list<role>)query.list(); homecoming roles; }

which gives me next error -

javax.el.elexception: org.hibernate.exception.sqlgrammarexception: not execute query @ org.jboss.el.util.reflectionutil.invokemethod(reflectionutil.java:332) @ org.jboss.el.util.reflectionutil.invokemethod(reflectionutil.java:273) @ org.jboss.el.parser.astmethodsuffix.getvalue(astmethodsuffix.java:59) @ org.jboss.el.parser.astvalue.getvalue(astvalue.java:67) @ org.jboss.el.valueexpressionimpl.getvalue(valueexpressionimpl.java:186) @ org.springframework.binding.expression.el.bindingvalueexpression.getvalue(bindingvalueexpression.java:54) @ org.springframework.binding.expression.el.elexpression.getvalue(elexpression.java:54) ... 58 more caused by: org.hibernate.exception.sqlgrammarexception: not execute query @ org.hibernate.exception.sqlstateconverter.convert(sqlstateconverter.java:90) @ org.hibernate.exception.jdbcexceptionhelper.convert(jdbcexceptionhelper.java:66) @ org.hibernate.loader.loader.dolist(loader.java:2235) @ org.hibernate.loader.loader.listignorequerycache(loader.java:2129) @ org.hibernate.loader.loader.list(loader.java:2124) @ org.hibernate.loader.hql.queryloader.list(queryloader.java:401) @ org.hibernate.hql.ast.querytranslatorimpl.list(querytranslatorimpl.java:363) @ org.hibernate.engine.query.hqlqueryplan.performlist(hqlqueryplan.java:196) @ org.hibernate.impl.sessionimpl.list(sessionimpl.java:1149) @ org.hibernate.impl.queryimpl.list(queryimpl.java:102) @ @ net.sf.cglib.proxy.methodproxy.invoke(methodproxy.java:149) @ org.springframework.aop.framework.cglib2aopproxy$cglibmethodinvocation.invokejoinpoint(cglib2aopproxy.java:692) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:150) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:107) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172) @ org.springframework.aop.framework.cglib2aopproxy$dynamicadvisedinterceptor.intercept(cglib2aopproxy.java:625) @ com.infoimage.infotrac.proofing.service.proofinglogservice$$enhancerbycglib$$d2fe9334.getrolebasedproofinglist(<generated>) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.jboss.el.util.reflectionutil.invokemethod(reflectionutil.java:328) ... 64 more caused by: java.sql.sqlexception: ora-00918: column ambiguously defined @ oracle.jdbc.driver.databaseerror.throwsqlexception(databaseerror.java:113) @ oracle.jdbc.driver.t4cttioer.processerror(t4cttioer.java:331) @ oracle.jdbc.driver.t4cttioer.processerror(t4cttioer.java:288) @ oracle.jdbc.driver.t4c8oall.receive(t4c8oall.java:754) @ oracle.jdbc.driver.t4cpreparedstatement.dooall8(t4cpreparedstatement.java:219) @ oracle.jdbc.driver.t4cpreparedstatement.executefordescribe(t4cpreparedstatement.java:813) @ oracle.jdbc.driver.oraclestatement.executemaybedescribe(oraclestatement.java:1051) @ oracle.jdbc.driver.t4cpreparedstatement.executemaybedescribe(t4cpreparedstatement.java:854) @ oracle.jdbc.driver.oraclestatement.doexecutewithtimeout(oraclestatement.java:1156) @ oracle.jdbc.driver.oraclepreparedstatement.executeinternal(oraclepreparedstatement.java:3415) @ oracle.jdbc.driver.oraclepreparedstatement.executequery(oraclepreparedstatement.java:3460) @ com.caucho.sql.userpreparedstatement.executequery(userpreparedstatement.java:108) @ org.hibernate.jdbc.abstractbatcher.getresultset(abstractbatcher.java:208) @ org.hibernate.loader.loader.getresultset(loader.java:1812) @ org.hibernate.loader.loader.doquery(loader.java:697) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:259) @ org.hibernate.loader.loader.dolist(loader.java:2232) ... 86 more

the equivalent oracle sql query selecting role -

select role_id appl_approval_roles client_id='clnt110' , appl_name='checking_statements';

the sql works fine. problem occurred hql. can help me? lot.

chaitanya answer.when tried reply gives- org.hibernate.hql.ast.querysyntaxexception: invalid path: 'client.id'error. utilize next hql (just bit modification of hql) gives me right result -

string hql = "select role applapprovalroles a.client.id = :clientid , a.applname = :applname";

hibernate orm entity hql pojo

No comments:

Post a Comment