java [Ljava.lang.Object; 不能被强制转换为错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29372228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[Ljava.lang.Object; cannot be cast to error
提问by Nadk
I get a list from my query.list()
. After that, I want to display the object inside this list but I got this error for this line for(Commerce[] c: this.newsList) {
我从我的query.list()
. 之后,我想在此列表中显示对象,但此行出现此错误for(Commerce[] c: this.newsList) {
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.model.Commerce; com.action.CommerceAction.searchCommerces(CommerceAction.java:35)
java.lang.ClassCastException: [Ljava.lang.Object; 不能转换为 [Lcom.model.Commerce; com.action.CommerceAction.searchCommerces(CommerceAction.java:35)
Here is my code:
这是我的代码:
My business service
我的企业服务
public List<Commerce[]> getCommercesBySearch(String categorie, String lieux) {
Query query = hibUtil.getSession().createQuery("from Commerce c, Categorie ca,Lieux l "
+ "where c.categorie=ca.idCategorie and c.lieux=l.idLieux and"
+ " ca.libelle='" + categorie + "' and l.ville='" + lieux + "' ");
List<Commerce[]> tuples = (List<Commerce[]>) query.list();
return tuples;
}
My action class
我的动作课
private CommerceService service;
private Commerce commerce = new Commerce();
private List<Commerce[]> newsList;
public String searchCommerces() {
String libelle = ServletActionContext.getRequest().getParameter("libelle");
String ville = ServletActionContext.getRequest().getParameter("ville");
this.newsList = service.getCommercesBySearch(libelle,ville);
for(Commerce[] c: this.newsList){
System.out.println(c[0].getNom());
}
if (this.newsList.size() > 0) {
return SUCCESS;
}
addActionError("Il n'existe aucun commerce de cette catégorie dans cette ville");
return ERROR;
}
回答by durron597
I am certain that this statement:
我确信这个声明:
List<Commerce[]> tuples = (List<Commerce[]>) query.list();
produces an unchecked type conversion warning. Your code is polluting the heapby doing this unchecked type conversion.query.list()
returns a raw List
, which will contain Object[]
. Here is the relevant Hibernate documentation:
产生未经检查的类型转换警告。通过执行这种未经检查的类型转换,您的代码正在污染堆。query.list()
返回一个 raw List
,其中将包含Object[]
. 这是相关的 Hibernate 文档:
Return the query results as a
List
. If the query contains multiple results per row, the results are returned in an instance ofObject[]
.
将查询结果作为
List
. 如果查询每行包含多个结果,则结果在 的实例中返回Object[]
。
Note that you cannotcast an array to an array of it's sub-type.
There are a couple of ways to fix this problem:
有几种方法可以解决此问题:
- Use
List<Object[]>
instead ofList<Commerce[]>
. You can then transform the result of thelist()
method into a more usable form, preferably within it's own method, before passing it on to the rest of your code. This is the preferable method if you need to select more than just theCommerce
object. - Add
SELECT c
to the beginning of your query, this will allow you to do a safeList<Commerce>
cast.
- 使用
List<Object[]>
代替List<Commerce[]>
。然后,您可以将list()
方法的结果转换为更有用的形式,最好是在它自己的方法中,然后再将其传递给您的其余代码。如果您需要选择的不仅仅是Commerce
对象,这是一种更可取的方法。 - 添加
SELECT c
到查询的开头,这将允许您进行安全转换List<Commerce>
。
回答by alias_boubou
Use select c
before your from
or you will have some trouble with the returned Object[]
. If you don't use this, Hibernate will return a list of Object[] containing more than one object into this. For your problem it will be like:
select c
在您之前使用,from
否则您将遇到返回的Object[]
. 如果不使用 this,Hibernate 将返回一个 Object[] 列表,其中包含多个对象到 this 中。对于您的问题,它将类似于:
Object[0]
is an instance ofCommerce
Object[1]
isCategorie
Object[3]
isLieux
- etc.
Object[0]
是一个实例Commerce
Object[1]
是Categorie
Object[3]
是Lieux
- 等等。
You will throw an ClassCastException
if you try to cast Object[]
into Commerce
.
你会抛出ClassCastException
,如果您尝试投Object[]
入Commerce
。
回答by Hoàng Long
I think the problem lies here:
我认为问题出在这里:
Query query = hibUtil.getSession().createQuery("from Commerce c, Categorie ca,Lieux l "
+ "where c.categorie=ca.idCategorie and c.lieux=l.idLieux and"
+ " ca.libelle='" + categorie + "' and l.ville='" + lieux + "' ");
List<Commerce[]> tuples = (List<Commerce[]>) query.list();
That's not correct, considering your idea. I believe that your query should return a List<Commerce>
, not List<Commerce[]>
.
考虑到你的想法,那是不正确的。我相信您的查询应该返回一个List<Commerce>
,而不是List<Commerce[]>
。
List<Commerce> tuples = (List<Commerce>) query.list();
To make that works, you need to add SELECT cin your query:
为了使它起作用,您需要在查询中添加SELECT c:
SELECT c from Commerce c, Categorie ca,Lieux l...
It will select a list of object Commerce
. If you leave the query like originally, it will return a list of Object array (actually they are Commerce[], Categorie[], Lieux[]...). Not to mention you can't cast directly an array in Java, the objects aren't the same type anyway.
它将选择一个对象列表Commerce
。如果您像原来一样离开查询,它将返回一个 Object 数组列表(实际上它们是 Commerce[]、Category[]、Lieux[]...)。更不用说您不能在 Java 中直接转换数组,无论如何,对象的类型都不相同。