我得到了引起:java.lang.ClassCastException:[Ljava.lang.Object; 不能投射到retail.model.vo.Book
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16262080/
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
iam getting Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to retail.model.vo.Book
提问by developer
I am getting the exception below when type casting the list to a particular object. I think I'm doing it correctly. Can anyone suggest what is the wrong with the code?
将列表类型转换为特定对象时,我收到以下异常。我想我做得对。任何人都可以建议代码有什么问题吗?
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;
}
}
回答by Aleksander Blomsk?ld
em.createQuery("select p,b from Products p,Book b where p.productId=b.productId").getResultList();
actually returns a List of object arrays (List<Object[]>
). The first element of the object array is a Product, and the second is a Book.
实际上返回一个对象数组列表 ( List<Object[]>
)。对象数组的第一个元素是 Product,第二个元素是 Book。
The compiler only complains with a warning in these cases because it doesn't know which generic type is returned from getResultList a priori. At runtime you don't get your ClassCastException at that line because of type erasure. The first actual cast of the Object[]
in the List
to the Book
is done on the first list.get.
编译器只在这些情况下发出警告,因为它不知道先验从 getResultList 返回哪种泛型类型。在运行时,由于类型擦除,您不会在该行收到 ClassCastException。的第一个实际投Object[]
中List
的Book
是第一list.get完成。
I guess the solution to your problem will be to change the query to:
我想您的问题的解决方案是将查询更改为:
List<Book> book = em.createQuery("select b from Products p,
Book b where p.productId=b.productId").getResultList();
Which actually will return a valid List of Books.
这实际上将返回一个有效的书籍列表。
回答by PSR
createQuery return list of objects here
createQuery 在此处返回对象列表
change
改变
List<Book> book = em.createQuery("select p,b from Products p,
Book b where p.productId=b.productId").getResultList();
to
到
List<Object[]> book = em.createQuery("select p,b from Products p,
Book b where p.productId=b.productId").getResultList();
回答by Abubakkar
If your query is returning a list of Object
arrays, the simply change this line:
如果您的查询返回Object
数组列表,只需更改此行:
List<Object> book = em.createQuery("select p,b from Products p,Book b where p.productId=b.productId").getResultList();
And as you making a cast in the loop it will be fine:
当您在循环中进行演员表时,它会很好:
Book books = (Book) book.get(i);
回答by Dmitri Algazin
Just for information, I got same problem with Neo4j objects transform using Spring to java entity
仅供参考,我在使用 Spring 到 Java 实体的 Neo4j 对象转换方面遇到了同样的问题
I had named query:
我命名了查询:
MATCH [bla-bla] RETURN a.name, a.associatedApplications
and the entity:
和实体:
@QueryResult
@XmlRootElement
public class SomeDescriptor {
@ResultColumn("a.name")
private String name ;
@ResultColumn("a.associatedApplications")
private List<String> associatedApplications = new ArrayList<String>();
}
Same error:
同样的错误:
Neo4j java.lang.classcastexception: [ljava.lang.string; cannot be cast to java.lang.iterable
After I used Neo4j COLLECT() function, all was working fine!
在我使用 Neo4j COLLECT() 函数后,一切正常!
Updated named query:
更新的命名查询:
RETURN a.name, COLLECT(a.associatedApplications) as associatedApplications
and updated the entity:
并更新实体:
@QueryResult
@XmlRootElement
public class SomeDescriptor {
@ResultColumn("a.name")
private String name ;
@ResultColumn("associatedApplications")
private List<String> associatedApplications = new ArrayList<String>();
}