سؤال

I have added category.java and CategoryFacade.java class to query the category table of my affablebean database. The category table contains two columns, i.e "id_cat" and "name". if i use <a href="category?${category.name}">in my index.jsp page it works well but in place of "name" when i use id_cat <a href="category?${category.id_cat}"> Its shows the error shown below. Could anyone please help me out with this problem. Below you can find all the related page code for your review.

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: The class 'entity.Category' does not have the property 'id_cat'.

root cause

javax.el.PropertyNotFoundException: The class 'entity.Category' does not have the property 'id_cat'.

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs.
GlassFish Server Open Source Edition 4.0

Here is my Category.java code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package entity;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author sabin
 */
@Entity
@Table(name = "category")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
    @NamedQuery(name = "Category.findByIdCat", query = "SELECT c FROM Category c WHERE c.idCat = :idCat"),
    @NamedQuery(name = "Category.findByName", query = "SELECT c FROM Category c WHERE c.name = :name")})
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id_cat")
    private Short idCat;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "categoryId")
    private Collection<Product> productCollection;

    public Category() {
    }

    public Category(Short idCat) {
        this.idCat = idCat;
    }

    public Category(Short idCat, String name) {
        this.idCat = idCat;
        this.name = name;
    }

    public Short getIdCat() {
        return idCat;
    }

    public void setIdCat(Short idCat) {
        this.idCat = idCat;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public Collection<Product> getProductCollection() {
        return productCollection;
    }

    public void setProductCollection(Collection<Product> productCollection) {
        this.productCollection = productCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idCat != null ? idCat.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Category)) {
            return false;
        }
        Category other = (Category) object;
        if ((this.idCat == null && other.idCat != null) || (this.idCat != null && !this.idCat.equals(other.idCat))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Category[ idCat=" + idCat + " ]";
    }

}

and here is my CategoryFacade.java code

<code>
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entity.Category;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author sabin
 */
@Stateless
public class CategoryFacade extends AbstractFacade<Category> {
    @PersistenceContext(unitName = "AffableBeanPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CategoryFacade() {
        super(Category.class);
    }

}

and here is my index.jsp page.

<div id="indexLeftColumn">
                <div id="welcomeText">
                    <p>[ welcome text ]</p>
                    categoryImagePath:${initParam.categoryImagePath}
                    productImagePath:${initParam.productImagePath}
                </div>
            </div>

            <div id="indexRightColumn">
                <c:forEach var="category" items="${categories}"> 
                     <div class="categoryBox">
                    <a href="category?${category.id_cat}">
                        <span class="categoryLabelText">${category.name}</span>
                        <img src="${initParam.categoryImagePath}${category.name}.jpg">
                    </a>
                </div>
                </c:forEach>
           </div>
هل كانت مفيدة؟

المحلول

After looking for solution and experimenting a lot with my code i finally found the solution. As you can see in my index.jsp code i have written category.id_cat.

<a href="category?${category.id_cat}">

One should be very careful while using netbeans wizard to create entiy class. My column name in table "category" is id_cat which changes to idCat in Category.java class. Though i thought its accepts the same name id_cat as mentioned in the Category.java class using annotation

 @Column(name="id_cat")

but i didnot see the variable declare just below,i.e.

private Short idCat

now to fetch the value i have to access idCat not id_cat. I replaced the code <a href="category?${category.id_cat}"> with <a href="category?${category}"> to see what column and values is being set in category variable from ${categories} result. I then found its using idCat=1 and then i went back to my code and found the variable in which the id_cat value from table was initialized. It was using idCat variable as i have mentioned above. I replaced id_cat with idCat and now its working perfectly.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top