3

将列表类型转换为特定对象时,出现以下异常。我想我做得对。谁能建议代码有什么问题?

SEVERE: Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to retail.model.vo.Book
SEVERE:     at retail.ejb.service.ProductsSessionBeanImpl.showBookDetails(ProductsSessionBeanImpl.java:40)
SEVERE:     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
SEVERE:     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
SEVERE:     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
SEVERE:     at java.lang.reflect.Method.invoke(Method.java:601)
SEVERE:     at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
SEVERE:     at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
SEVERE:     at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
SEVERE:     at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
SEVERE:     at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
SEVERE:     at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
SEVERE:     at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
SEVERE:     at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
SEVERE:     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
SEVERE:     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
SEVERE:     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
SEVERE:     at java.lang.reflect.Method.invoke(Method.java:601)
SEVERE:     at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
SEVERE:     at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
SEVERE:     at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
SEVERE:     at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360)
SEVERE:     at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
SEVERE:     at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:206)
SEVERE:     ... 54 more


@Override
        public List<Book> showBookDetails() {
            List<Book> book = em.createQuery("select p,b from Products p,Book b where p.productId=b.productId").getResultList();
            //PRODUCT,book where PRODUCT.PRODUCT_ID = book.product_id
            System.out.println("List Size:::"+ book.size());
            //Customer obj = new Customer();
            List<Book> booksList = new ArrayList<Book>(); 
            for(int i=0; i<book.size();i++){




                Book books = (Book) book.get(i);
                System.out.println("Author ::::" + books.getAuthor() + "::ISBN ::"+books.getIsbnNumber()+"::pages::"+books.getNoOfpages()+"::price::"+books.getPrice()+
                        ":::description::"+books.getProductDesc() + "::product id ::"+books.getProductId()+"::title::"+books.getProductTitle()+"::Stock::"+books.getStock());
                booksList.add(books);
                //obj.setCustomerList(customersList);
            }
            return booksList;
        }

    }


package retail.model.vo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
@PrimaryKeyJoinColumn(name="product_id")

public class Book extends Products implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 4264546498700495061L;

    private String author;
    private String isbnNumber;
    private int noOfpages;
    private String illustrations;

    @Column(name = "author")
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    @Column(name = "isbn_number")
    public String getIsbnNumber() {
        return isbnNumber;
    }
    public void setIsbnNumber(String isbnNumber) {
        this.isbnNumber = isbnNumber;
    }

    @Column(name = "noof_pages")
    public int getNoOfpages() {
        return noOfpages;
    }
    public void setNoOfpages(int noOfpages) {
        this.noOfpages = noOfpages;
    }

    @Column(name = "illustrations")
    public String getIllustrations() {
        return illustrations;
    }
    public void setIllustrations(String illustrations) {
        this.illustrations = illustrations;
    }



}
4

4 回答 4

6
em.createQuery("select p,b from Products p,Book b where p.productId=b.productId").getResultList(); 

实际上返回一个对象数组列表 ( List<Object[]>)。对象数组的第一个元素是 Product,第二个元素是 Book。

在这些情况下,编译器只会发出警告,因为它不知道从 getResultList 先验返回的泛型类型。在运行时,由于类型擦除,您不会在该行获得 ClassCastException。中的第一个实际转换是Object[]在第一个 list.get 上完成的。ListBook

我想您的问题的解决方案是将查询更改为:

List<Book> book = em.createQuery("select b from Products p,
 Book b where p.productId=b.productId").getResultList();

这实际上会返回一个有效的书籍列表。

于 2013-04-28T10:54:04.637 回答
1

createQuery 在此处返回对象列表

改变

 List<Book> book = em.createQuery("select p,b from Products p,
 Book b where p.productId=b.productId").getResultList();

  List<Object[]> book = em.createQuery("select p,b from Products p,
 Book b where p.productId=b.productId").getResultList();
于 2013-04-28T10:54:55.963 回答
1

如果您的查询返回Object数组列表,只需更改此行:

List<Object> book = em.createQuery("select p,b from Products p,Book b where p.productId=b.productId").getResultList();

当您在循环中进行强制转换时,它会很好:

Book books = (Book) book.get(i);
于 2013-04-28T10:57:08.850 回答
0

仅供参考,我在使用 Spring 将 Neo4j 对象转换为 java 实体时遇到了同样的问题

我命名了查询:

 MATCH [bla-bla] RETURN a.name, a.associatedApplications

和实体:

@QueryResult
@XmlRootElement
public class SomeDescriptor {
    @ResultColumn("a.name")
    private String name ;

    @ResultColumn("a.associatedApplications")
    private List<String> associatedApplications = new ArrayList<String>();
}

同样的错误:

    Neo4j java.lang.classcastexception: [ljava.lang.string; cannot be cast to java.lang.iterable

在我使用 Neo4j COLLECT() 函数后,一切正常!

更新的命名查询:

 RETURN a.name, COLLECT(a.associatedApplications) as associatedApplications

并更新了实体:

@QueryResult
@XmlRootElement
public class SomeDescriptor {
    @ResultColumn("a.name")
    private String name ;

    @ResultColumn("associatedApplications")
    private List<String> associatedApplications = new ArrayList<String>();
}
于 2015-03-06T13:46:15.480 回答