本文介绍了Hibernate LeftOuter加入HQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的左连接 hql查询。执行此代码后,我得到列表大小。

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

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

我收到以下错误:

  java.lang.ClassCastException:[Ljava.lang.Object;不能转换为org.bluewhale.model.BwClientdetails 
at testapplication.Main.getClients(Main.java:364)
at testapplication.Main.main(Main.java:54)


解决方案

通过不指定Select事件,查询的结果是一个Array BwClientdetails,bwClientAllocations。
在查询前添加选择客户端应该可以解决您的问题

 从BwClientdetails客户端中选择客户端左连接client.bwClientAllocations 

或者替换为

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

这是最好的做法,总是指定一个where子句,它甚至是JPA规范

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)
解决方案

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

这篇关于Hibernate LeftOuter加入HQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:43