facebook

java.lang.classCastException in hibernate

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #271011 Reply

    datapower
    Member

    This is the error message in Weblogic 8.1.
    MyEclipse Enterprise Workbench

    Version: 5.0.1 GA
    Build id: 20060810-5.0.1-GA
    *******************************************************************************************************
    java.lang.ClassCastException
    at com.cyberize.hibernate3.ProductsDAO.findAll(ProductsDAO.java:67
    )
    at com.cyberize.struts.action.ProductListAction.execute(ProductListAction.java:45)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6718)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
    at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3764)
    at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2644)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
    ***************************************************************************************************
    I used the reverse engineering in Myeclipse to generate the POJOs and DAOs:
    Here is the code for the findAll() in my DAO(I added this method):

    public Products[] findAll() {
    log.debug(“finding ALL Products “);
    List products = new ArrayList();
    try {
    List results = getSession()
    .createCriteria(“Products”)
    .list();
    log.debug(“find All products successful, result size: ” + results.size());
    /** convert the List of objects to an ArrayList of Products objects */
    for (Iterator iter = results.iterator(); iter.hasNext();) {
    products.add((Products) iter.next());
    }
    /** convert the ArrayList to an Array of Products */
    return (Products[]) products.toArray(new Products[0]);
    } catch (RuntimeException re) {
    log.error(“find All products failed”, re);
    throw re;
    }
    }
    **************************************************************************************************
    The line in bold where i cast an iter.next() to my Products pojo is the culprit. Basically I can’t cast from an object class(iter.next() returns object) to my pojo named Products.
    Here is my Products.java generated by reverse engineering tool:
    ***************************************************************************************************
    package com.cyberize.hibernate3;
    // Generated by MyEclipse – Hibernate Tools

    import java.util.Date;
    import java.util.Set;

    /**
    * Products generated by MyEclipse – Hibernate Tools
    */
    public class Products extends AbstractProducts implements java.io.Serializable {

    // Constructors

    /** default constructor */
    public Products() {
    }

    /** minimal constructor */
    public Products(RefProductTypes refProductTypes, Suppliers suppliers) {
    super(refProductTypes, suppliers);
    }

    /** full constructor */
    public Products(RefProductTypes refProductTypes, Suppliers suppliers, Double productPrice, String bookIsbn, String bookAuthor, Date bookPublicationDate, String bookTitle, Double bookPrice, String foodContainsYn, String foodName, String foodDescription, String foodFlavor, String foodIngredients, String otherProductDetails, Set customerOrdersProductses) {
    super(refProductTypes, suppliers, productPrice, bookIsbn, bookAuthor, bookPublicationDate, bookTitle, bookPrice, foodContainsYn, foodName, foodDescription, foodFlavor, foodIngredients, otherProductDetails, customerOrdersProductses);
    }

    }
    ***********************************************************************************************************
    and here is the abstract class (super class of my Products pojo) the reverse enginnering tool created:
    package com.cyberize.hibernate3;

    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;

    /**
    * AbstractProducts generated by MyEclipse – Hibernate Tools
    */

    public abstract class AbstractProducts implements java.io.Serializable {
    // Fields

    private Integer productId;
    private RefProductTypes refProductTypes;
    private Suppliers suppliers;
    private Double productPrice;
    private String bookIsbn;
    private String bookAuthor;
    private Date bookPublicationDate;
    private String bookTitle;
    private Double bookPrice;
    private String foodContainsYn;
    private String foodName;
    private String foodDescription;
    private String foodFlavor;
    private String foodIngredients;
    private String otherProductDetails;
    private Set customerOrdersProductses = new HashSet(0);

    // Constructors

    /** default constructor */
    public AbstractProducts() {
    }

    /** minimal constructor */
    public AbstractProducts(RefProductTypes refProductTypes, Suppliers suppliers) {
    this.refProductTypes = refProductTypes;
    this.suppliers = suppliers;
    }

    /** full constructor */
    public AbstractProducts(RefProductTypes refProductTypes, Suppliers suppliers, Double productPrice, String bookIsbn, String bookAuthor, Date bookPublicationDate, String bookTitle, Double bookPrice, String foodContainsYn, String foodName, String foodDescription, String foodFlavor, String foodIngredients, String otherProductDetails, Set customerOrdersProductses) {
    this.refProductTypes = refProductTypes;
    this.suppliers = suppliers;
    this.productPrice = productPrice;
    this.bookIsbn = bookIsbn;
    this.bookAuthor = bookAuthor;
    this.bookPublicationDate = bookPublicationDate;
    this.bookTitle = bookTitle;
    this.bookPrice = bookPrice;
    this.foodContainsYn = foodContainsYn;
    this.foodName = foodName;
    this.foodDescription = foodDescription;
    this.foodFlavor = foodFlavor;
    this.foodIngredients = foodIngredients;
    this.otherProductDetails = otherProductDetails;
    this.customerOrdersProductses = customerOrdersProductses;
    }

    // Property accessors

    public Integer getProductId() {
    return this.productId;
    }

    public void setProductId(Integer productId) {
    this.productId = productId;
    }

    public RefProductTypes getRefProductTypes() {
    return this.refProductTypes;
    }

    public void setRefProductTypes(RefProductTypes refProductTypes) {
    this.refProductTypes = refProductTypes;
    }

    public Suppliers getSuppliers() {
    return this.suppliers;
    }

    public void setSuppliers(Suppliers suppliers) {
    this.suppliers = suppliers;
    }

    public Double getProductPrice() {
    return this.productPrice;
    }

    public void setProductPrice(Double productPrice) {
    this.productPrice = productPrice;
    }

    public String getBookIsbn() {
    return this.bookIsbn;
    }

    public void setBookIsbn(String bookIsbn) {
    this.bookIsbn = bookIsbn;
    }

    public String getBookAuthor() {
    return this.bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
    this.bookAuthor = bookAuthor;
    }

    public Date getBookPublicationDate() {
    return this.bookPublicationDate;
    }

    public void setBookPublicationDate(Date bookPublicationDate) {
    this.bookPublicationDate = bookPublicationDate;
    }

    public String getBookTitle() {
    return this.bookTitle;
    }

    public void setBookTitle(String bookTitle) {
    this.bookTitle = bookTitle;
    }

    public Double getBookPrice() {
    return this.bookPrice;
    }

    public void setBookPrice(Double bookPrice) {
    this.bookPrice = bookPrice;
    }

    public String getFoodContainsYn() {
    return this.foodContainsYn;
    }

    public void setFoodContainsYn(String foodContainsYn) {
    this.foodContainsYn = foodContainsYn;
    }

    public String getFoodName() {
    return this.foodName;
    }

    public void setFoodName(String foodName) {
    this.foodName = foodName;
    }

    public String getFoodDescription() {
    return this.foodDescription;
    }

    public void setFoodDescription(String foodDescription) {
    this.foodDescription = foodDescription;
    }

    public String getFoodFlavor() {
    return this.foodFlavor;
    }

    public void setFoodFlavor(String foodFlavor) {
    this.foodFlavor = foodFlavor;
    }

    public String getFoodIngredients() {
    return this.foodIngredients;
    }

    public void setFoodIngredients(String foodIngredients) {
    this.foodIngredients = foodIngredients;
    }

    public String getOtherProductDetails() {
    return this.otherProductDetails;
    }

    public void setOtherProductDetails(String otherProductDetails) {
    this.otherProductDetails = otherProductDetails;
    }

    public Set getCustomerOrdersProductses() {
    return this.customerOrdersProductses;
    }

    public void setCustomerOrdersProductses(Set customerOrdersProductses) {
    this.customerOrdersProductses = customerOrdersProductses;
    }

    ******************************************************************************************************
    It seems that the POJOs are not subclass of object class .

    Any help would be greatly appreciated.
    Thank You

    #271107 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev

    I would suggest setting a breakpoint and seeing what type of object iter is returning with the call to next()

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: java.lang.classCastException in hibernate

You must be logged in to post in the forum log in