com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'where子句'中的未知列'productId3'

时间:2012-06-18 09:53:27

标签: java database spring hibernate hql

尝试运行此查询时出现错误com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'productId3' in 'where clause'

public Vendor getVendorId(ProductInfo productInfo) { 
        return (Vendor) this.sessionFactory
                .getCurrentSession()
                .createQuery(
                        "select vendorId from Product where productId3 = :id")
                .setParameter("id", productInfo).uniqueResult();
    }

产品类看起来像这样:

@Entity
@Table(name="PRODUCT")
public class Product{

    @Id
    @GeneratedValue
    @Column(name="PRODUCT_ID2")
    private Integer productId2;

    public void setProductId2(Integer productId2){
        this.productId2 = productId2;
    }

    public Integer getProductId2(){
        return productId2;
    }

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="PRODUCT_ID3")
    private ProductInfo productId3;

    public void setProductId3(ProductInfo productId3){
        this.productId3 = productId3;
    }

    public ProductInfo getProductId3(){
        return productId3;
    }

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="VENDOR_ID")
    private Vendor vendorId;

    public void setVendorId(Vendor vendorId){
        this.vendorId = vendorId;
    }

    public Vendor getVendorId(){
        return vendorId;
    }

    @OneToMany(mappedBy="productId2")
    private Set<ProductRevision> productRevisions;

    public void setProductRevisions(Set<ProductRevision> productRevisions){
        this.productRevisions = productRevisions;
    }

    public Set<ProductRevision> getProductRevisions(){
        return productRevisions;
    }
}

为什么会出现此错误?我不明白为什么它找不到列,因为名称完全相同。谢谢你的帮助!

/ d

2 个答案:

答案 0 :(得分:1)

使用此查询:

select p.vendorId from Product p where p.productId3 = :id

答案 1 :(得分:0)

原谅挑剔的回复,但HQL更清楚的一件事就是重命名Product类的字段。

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="PRODUCT_ID3")
    private ProductInfo productInfo;

    public void setProductInfo(ProductInfo productInfo){
        this.productInfo = productInfo;
    }

同样适用于vendorId字段。然后你的HQL读得更好。很明显,您正在处理实体而不是sql列。

    public Vendor getVendorId(ProductInfo productInfo) { 
        return (Vendor) this.sessionFactory
            .getCurrentSession()
            .createQuery(
                    "select p.vendor from Product where productInfo = :productInfo")
            .setParameter("productInfo", productInfo).uniqueResult();
    }
相关问题