问题
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