MySQLSyntaxErrorException:未知列'类别'在'字段列表'

时间:2016-04-26 11:33:09

标签: java hibernate jpa import

我一直在为我的在线商店项目创建购物车。我有一张名为" products"和此表中的实体类如下;

package entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author fet
 */

@Entity

@Table(name = "products")

@NamedQueries({

    @NamedQuery(name = "Products.findAll", query = "SELECT p FROM Products p"),
    @NamedQuery(name = "Products.findByProductId", query = "SELECT p FROM Products p WHERE p.productId = :productId"),
    @NamedQuery(name = "Products.findByProductName", query = "SELECT p FROM Products p WHERE p.productName = :productName"),
    @NamedQuery(name = "Products.findBySubCategoryName", query = "SELECT p FROM Products p WHERE p.subCategoryName = :subCategoryName"),
    @NamedQuery(name = "Products.findByCategoryName", query = "SELECT p FROM Products p WHERE p.categoryName = :categoryName"),
    @NamedQuery(name = "Products.findByCompanyName", query = "SELECT p FROM Products p WHERE p.companyName = :companyName"),
    @NamedQuery(name = "Products.findByPrice", query = "SELECT p FROM Products p WHERE p.price = :price"),
    @NamedQuery(name = "Products.findByTags", query = "SELECT p FROM Products p WHERE p.tags = :tags"),
    @NamedQuery(name = "Products.findByProductQty", query = "SELECT p FROM Products p WHERE p.productQty = :productQty"),
    @NamedQuery(name = "Products.findByLastUpdated", query = "SELECT p FROM Products p WHERE p.lastUpdated = :lastUpdated"),
    @NamedQuery(name = "Products.findByHits", query = "SELECT p FROM Products p WHERE p.hits = :hits")})
public class Products implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "product_id")
    private Integer productId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 60)
    @Column(name = "product-name")
    private String productName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 40)
    @Column(name = "sub-category-name")
    private String subCategoryName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 40)
    @Column(name = "category-name")
    private String categoryName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 40)
    @Column(name = "company-name")
    private String companyName;
    @Basic(optional = false)
    @NotNull
    @Column(name = "price")
    private double price;
    @Lob
    @Size(max = 65535)
    @Column(name = "summary")
    private String summary;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "tags")
    private String tags;
    @Basic(optional = false)
    @NotNull
    @Column(name = "product_qty")
    private int productQty;
    @Basic(optional = false)
    @NotNull
    @Column(name = "lastUpdated")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdated;
    @Basic(optional = false)
    @NotNull
    @Column(name = "hits")
    private int hits;

    public Products() {
    }

    public Products(Integer productId) {
        this.productId = productId;
    }


    public Products(Integer productId, String productName, String subCategoryName, String categoryName, String companyName, double price, String tags, int productQty, Date lastUpdated, int hits) {
        this.productId = productId;
        this.productName = productName;
        this.subCategoryName = subCategoryName;
        this.categoryName = categoryName;
        this.companyName = companyName;
        this.price = price;
        this.tags = tags;
        this.productQty = productQty;
        this.lastUpdated = lastUpdated;
        this.hits = hits;
    }

    public Integer getProductId() {
        return productId;
    }

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

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getSubCategoryName() {
        return subCategoryName;
    }

    public void setSubCategoryName(String subCategoryName) {
        this.subCategoryName = subCategoryName;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public int getProductQty() {
        return productQty;
    }

    public void setProductQty(int productQty) {
        this.productQty = productQty;
    }

    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public int getHits() {
        return hits;
    }

    public void setHits(int hits) {
        this.hits = hits;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (productId != null ? productId.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 Products)) {
            return false;
        }
        Products other = (Products) object;
        if ((this.productId == null && other.productId != null) || (this.productId != null && !this.productId.equals(other.productId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Products[ productId=" + productId + " ]";
    }
}

为了处理操作,我有一个名为"操作"的ejb类。如下;

package ejb;

import entities.Products;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author fet
 */

@Stateless
public class operations {

    @PersistenceContext(unitName = "registerPU")
    private EntityManager em;


    public List<Products> retrieveProducts() {

        return  em.createQuery("SELECT p FROM Products p").getResultList();
    }
}

我有一个xhtml文件,我想展示我的产品;

<h:dataTable value="#{index.products}" var="p">

        <h:column>

            <f:facet name="header" />
            <h:outputLink value="index.xhtml"/>
            <f:param   name="query" value="#{p.productId}"/>

        </h:column>
        .
        .
        .
        .
        .
    </h:dataTable>

最后我有RequestScoped bean命名为index;

package WEB;

import ejb.operations;
import entities.Products;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

/**
 *
 * @author fet
 */
@Named(value = "index")
@RequestScoped

public class index implements Serializable {

    @EJB
    private operations operations;

    public index() {
    }

    public List<Products> getProducts() {
        return operations.retrieveProducts();
    }
}

但我立刻收到了这个错误:

  

HTTP状态500 - 内部服务器错误
  类型异常报告
  messageInternal Server Error
  description服务器遇到内部错误,导致无法完成此请求   例外
  javax.servlet.ServletException       根本原因       javax.ejb.EJBException异常       根本原因       javax.persistence.PersistenceException:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.6.1.v20150605-31e8258):       org.eclipse.persistence.exceptions.DatabaseException

Internal Exception: 

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'category' in 'field list'

Error Code: 1054

Call: SELECT product_id, category-name, company-name, hits, lastUpdated, price, product-name, product_qty, sub-category-name, summary, tags FROM products

Query: ReadAllQuery(referenceClass=Products sql="SELECT product_id, category-name, company-name, hits, lastUpdated, price, product-name, product_qty, sub-category-name, summary, tags FROM products")

root cause

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException

注意GlassFish Server Open Source Edition 4.1.1日志中提供了异常的完整堆栈跟踪及其根本原因。

有任何意见或建议吗?提前致谢

1 个答案:

答案 0 :(得分:1)

这是因为您在列名中使用连字符( - )。尝试用下划线(_)替换它 我不确定你是否可以这样逃避它:

@Column(name = "\`category-name\`")  
private String categoryName;