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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:08:02  来源:igfitidea点击:

[Ljava.lang.Object; cannot be cast to error

javahibernate

提问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 of Object[].

将查询结果作为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:

有几种方法可以解决此问题:

  1. Use List<Object[]>instead of List<Commerce[]>. You can then transform the result of the list()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 the Commerceobject.
  2. Add SELECT cto the beginning of your query, this will allow you to do a safe List<Commerce>cast.
  1. 使用List<Object[]>代替List<Commerce[]>。然后,您可以将list()方法的结果转换为更有用的形式,最好是在它自己的方法中,然后再将其传递给您的其余代码。如果您需要选择的不仅仅是Commerce对象,这是一种更可取的方法。
  2. 添加SELECT c到查询的开头,这将允许您进行安全转换List<Commerce>

回答by alias_boubou

Use select cbefore your fromor 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 of Commerce
  • Object[1]is Categorie
  • Object[3]is Lieux
  • etc.
  • Object[0]是一个实例 Commerce
  • Object[1]Categorie
  • Object[3]Lieux
  • 等等。

You will throw an ClassCastExceptionif 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 中直接转换数组,无论如何,对象的类型都不相同。