将列表类型转换为特定对象时,出现以下异常。我想我做得对。谁能建议代码有什么问题?
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;
}
}