【问题标题】:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'productId3' in 'where clause'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“where 子句”中的未知列“productId3”
【发布时间】:2012-06-18 09:53:27
【问题描述】:

我在尝试运行此查询时收到错误 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();
    }

Product 类如下所示:

@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

【问题讨论】:

  • 您可以先将 showSql 参数设置为 1,然后检查输出查询并在此处发布。

标签: java database spring hibernate hql


【解决方案1】:

使用这个查询:

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

【讨论】:

【解决方案2】:

请原谅您的繁琐回复,但您可能会更清楚 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();
    }

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2011-12-15
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    相关资源
    最近更新 更多