iam getting Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to retail.model.vo.Book
generics, java, jpa
Solution
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.
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.
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.
Problem
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; } } ```