代码之家  ›  专栏  ›  技术社区  ›  Dave

org.hibernate.hql.ast.querySyntaxException:产品未映射[来自产品]

  •  0
  • Dave  · 技术社区  · 14 年前

    我今天最恼人的问题是:

    我正在尝试使用Hibernate访问数据库。在多次错误启动之后,我放弃了我的特定项目,选择使用NetBeans 6.9附带的示例数据库和向导。

    我启动了NetBeans,启动了示例Derby数据库,并按照本教程中的说明进行操作: http://netbeans.org/kb/docs/java/hibernate-java-se.html ,跳过mysql数据库的位,用信息替换derby sample 数据库(如适用)。

    我所有的谷歌搜索都告诉我,我犯了一个新手错误,我指的是表名而不是类名。我确信事实并非如此。引用一个类的方法只有这么多。除此之外,我很困惑。除了巫师,我什么都没用,我使用了示例数据库,我找不到任何其他我可能搞砸的东西。

    我右击 hibernate.cfg.xml 选择 Run HQL Query (就像在教程中一样)并在运行查询时在上面的问题标题中获取异常 from Product

    请在我肝衰竭前帮助我。

    [1]这就是你们中的一个会证明我错的地方。

    休眠.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>
    

    产品HB.MXML

    <?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>
    

    爪哇产品

    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;
        }
    
    
    
    
    }
    
    2 回复  |  直到 13 年前
        1
  •  0
  •   Dave    14 年前

    netbeans hql查询运行程序似乎已损坏。我听从帕斯卡·蒂凡特的建议,一切都很好。

    如果有人发现自己也有同样的问题,并提出了解决办法,请把它贴在这里,我会把它标为答案。

        2
  •  0
  •   WarFox    13 年前

    我也有同样的问题-从Hibernate获取[ClassName]未映射异常。

    但我在阅读您的文章后发现的一个不同点是,当右键单击hibernate.cfg.xml运行时,我没有得到异常,从nb中选择run hql query。查询运行时没有任何错误/警告。

    然后我检查了我的代码,发现有一个 config.addclass()中的愚蠢拼写错误 我的代码中的行。纠正它解决了我的问题。