2010-08-30 29 views
0

我一天的真氣問題是這樣的:org.hibernate.hql.ast.QuerySyntaxException:該產品不會映射[從產品]

我嘗試使用Hibernate來訪問數據庫。經過一系列錯誤的開始,我放棄了我的特定項目,並選擇使用Netbeans 6.9附帶的示例數據庫和嚮導。

我解僱了NetBeans中,開始樣品Derby數據庫,並遵循本教程中的說明:http://netbeans.org/kb/docs/java/hibernate-java-se.html,跳過關於MySQL數據庫和信息代位爲德比sample數據庫在適當情況下。

我的所有Google使用告訴我,我正在犯一個菜鳥的錯誤,指的是表名而不是類名。我確信自己並非如此[1]。只有很多方法可以參考一堂課。除此之外,我很困惑。除了嚮導之外,我只使用過示例數據庫,除此之外我什麼都找不到。

我右鍵點擊hibernate.cfg.xml,選擇Run HQL Query(就像在教程中),並獲得當我運行查詢from Product

請幫我我的肝給了之前在上面的問題的標題除外。

[1]這是你們其中一個會證明我錯了的地方。

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property> 
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> 
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property> 
    <property name="hibernate.connection.username">app</property> 
    <property name="hibernate.connection.password">app</property> 
<property name="hibernate.show_sql">true</property> 
<mapping resource="sample/db/PurchaseOrder.hbm.xml"/> 
<mapping resource="sample/db/Customer.hbm.xml"/> 
<mapping resource="sample/db/MicroMarket.hbm.xml"/> 
<mapping resource="sample/db/ProductCode.hbm.xml"/> 
<mapping resource="sample/db/Product.hbm.xml"/> 
<mapping resource="sample/db/Manufacturer.hbm.xml"/> 
<mapping resource="sample/db/DiscountCode.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

Product.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated Aug 30, 2010 3:57:50 PM by Hibernate Tools 3.2.1.GA --> 
<hibernate-mapping> 
    <class name="sample.db.Product" schema="APP" table="PRODUCT"> 
    <id name="productId" type="int"> 
     <column name="PRODUCT_ID"/> 
     <generator class="assigned"/> 
    </id> 
    <property name="manufacturerId" type="int"> 
     <column name="MANUFACTURER_ID" not-null="true"/> 
    </property> 
    <property name="productCode" type="string"> 
     <column length="2" name="PRODUCT_CODE" not-null="true"/> 
    </property> 
    <property name="purchaseCost" type="big_decimal"> 
     <column name="PURCHASE_COST" precision="12"/> 
    </property> 
    <property name="quantityOnHand" type="java.lang.Integer"> 
     <column name="QUANTITY_ON_HAND"/> 
    </property> 
    <property name="markup" type="big_decimal"> 
     <column name="MARKUP" precision="4"/> 
    </property> 
    <property name="available" type="string"> 
     <column length="5" name="AVAILABLE"/> 
    </property> 
    <property name="description" type="string"> 
     <column length="50" name="DESCRIPTION"/> 
    </property> 
    </class> 
</hibernate-mapping> 

Product.java

package sample.db; 
// Generated Aug 30, 2010 3:57:50 PM by Hibernate Tools 3.2.1.GA 


import java.math.BigDecimal; 

/** 
* Product generated by hbm2java 
*/ 
public class Product implements java.io.Serializable { 


    private int productId; 
    private int manufacturerId; 
    private String productCode; 
    private BigDecimal purchaseCost; 
    private Integer quantityOnHand; 
    private BigDecimal markup; 
    private String available; 
    private String description; 

    public Product() { 
    } 


    public Product(int productId, int manufacturerId, String productCode) { 
     this.productId = productId; 
     this.manufacturerId = manufacturerId; 
     this.productCode = productCode; 
    } 
    public Product(int productId, int manufacturerId, String productCode, BigDecimal purchaseCost, Integer quantityOnHand, BigDecimal markup, String available, String description) { 
     this.productId = productId; 
     this.manufacturerId = manufacturerId; 
     this.productCode = productCode; 
     this.purchaseCost = purchaseCost; 
     this.quantityOnHand = quantityOnHand; 
     this.markup = markup; 
     this.available = available; 
     this.description = description; 
    } 

    public int getProductId() { 
     return this.productId; 
    } 

    public void setProductId(int productId) { 
     this.productId = productId; 
    } 
    public int getManufacturerId() { 
     return this.manufacturerId; 
    } 

    public void setManufacturerId(int manufacturerId) { 
     this.manufacturerId = manufacturerId; 
    } 
    public String getProductCode() { 
     return this.productCode; 
    } 

    public void setProductCode(String productCode) { 
     this.productCode = productCode; 
    } 
    public BigDecimal getPurchaseCost() { 
     return this.purchaseCost; 
    } 

    public void setPurchaseCost(BigDecimal purchaseCost) { 
     this.purchaseCost = purchaseCost; 
    } 
    public Integer getQuantityOnHand() { 
     return this.quantityOnHand; 
    } 

    public void setQuantityOnHand(Integer quantityOnHand) { 
     this.quantityOnHand = quantityOnHand; 
    } 
    public BigDecimal getMarkup() { 
     return this.markup; 
    } 

    public void setMarkup(BigDecimal markup) { 
     this.markup = markup; 
    } 
    public String getAvailable() { 
     return this.available; 
    } 

    public void setAvailable(String available) { 
     this.available = available; 
    } 
    public String getDescription() { 
     return this.description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 




} 
+0

我不會相信奇才。嘗試運行該應用程序(再次),並分享您得到的異常。 – Bozho 2010-08-30 23:37:54

+0

您使用Hibernate映射文件使用JPA批註的任何特定原因?幾年來,NetBeans嚮導爲我提供了良好的JPA映射。 – 2010-08-30 23:52:58

+0

'SessionFactory'創建時可能存在錯誤,導致Product無法正確映射。以編程方式創建它並查看出現的錯誤(併發布跟蹤。 – 2010-08-31 01:20:41

回答

0

N個etbeans HQL查詢跑步似乎被打破。我遵循Pascal Thivent的建議,一切正常。

如果有人發現自己遇到同樣的問題並提出修復方案,請將其張貼在此處,並將其標記爲答案。

0

我有同樣的問題 - 從休眠獲取[classname]未映射異常。

但是在閱讀您的文章後發現的一個差異是,我沒有通過右鍵單擊hibernate.cfg.xml運行異常,從NB中選擇運行HQL查詢。查詢運行時沒有任何錯誤/警告。

然後我檢查了我的代碼,發現在我的代碼中有一個config.addClass()行中的傻拼寫錯誤。更正解決了我的問題。