2012-06-18 56 views
0

试图运行此查询时,我得到一个错误com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'productId3' in 'where clause'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列“productId3”在“where子句”

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

+0

您可以通过将showSql参数设置为1并检查输出查询并在此处发布来开始。 – MahdeTo

回答

1

使用此查询:

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

顺便说一句,如果你不介意,你能解释为什么我最初的查询不起作用吗?谢谢! – dlinx90

+0

SQL查询不会像Hibernate/JPA一样工作,请参阅http://en.wikipedia.org/wiki/Java_Persistence_Query_Language – Subin

0

原谅繁琐的响应,但有一件事你是对Product类重命名你的领域可能HQL清晰。

@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(); 
    } 
相关问题