Hibernate LeftOuter join HQL

本小妞迷上赌 提交于 2020-01-04 05:32:46

问题


This is my left join hql query. After executing this code i am getting list size. But unable cast object to respective pojo class.

Query query=session.createQuery("from BwClientdetails client left join client.bwClientAllocations");

System.out.println(">>>"+query.list().size());
List<BwClientdetails> list=query.list();
for(int i=0;i<list.size();i++){
   BwClientdetails bc=list.get(i);
   System.out.println(bc.getClientid());
}

I am getting below error:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.bluewhale.model.BwClientdetails
        at testapplication.Main.getClients(Main.java:364)
        at testapplication.Main.main(Main.java:54)

回答1:


By not specifing a Select case, the result of your query is an Array of BwClientdetails, bwClientAllocations. Adding Select client in front of the query should solve your problem

Select client from  BwClientdetails  client left  join client.bwClientAllocations

or replacing your for by

for(int i=0;i<list.size();i++){
   BwClientdetails bc=list.get(i)[0];
   System.out.println(bc.getClientid());
}

It's a best practice to alway specify a where clause, it is even part of the JPA spec



来源:https://stackoverflow.com/questions/5922768/hibernate-leftouter-join-hql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!