我当天真气的问题是:
我正在尝试使用Hibernate来访问数据库。经过一系列错误的启动后,我放弃了我的特定项目,并选择使用Netbeans 6.9附带的示例数据库和向导。
我启动了Netbeans,启动了示例Derby数据库,并按照本教程中的说明进行操作:http://netbeans.org/kb/docs/java/hibernate-java-se.html,跳过有关MySQL数据库的信息,并在适当的情况下用Derby sample
数据库替换信息。
我的所有谷歌搜索都告诉我,我正在犯一个菜鸟的错误并提到表名而不是类名。我确信自己并非如此[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 :(得分:0)
Netbeans HQL Query运行器似乎已被打破。我按照Pascal Thivent的建议,一切正常。
如果其他人发现自己遇到了同样的问题并提出了修复方法,请在此处发布,我会将其标记为答案。
答案 1 :(得分:0)
我遇到了同样的问题 - 让[classname]没有映射来自hibernate的异常。
但是我在阅读你的帖子后发现的一个不同之处是,当你通过右键单击hibernate.cfg.xml运行时,我没有得到异常,从NB中选择Run HQL Query。查询运行时没有任何错误/警告。
然后我检查了我的代码,发现我的代码中的config.addClass()行中存在愚蠢的拼写错误。纠正这解决了我的问题。