我目前正在尝试学习使用 Hibernate 将对象存储在数据库中。我一直在学习https://netbeans.org/kb/docs/java/hibernate-java-se.html上的教程,我正在尝试为一个非常简单的类和表创建一个 XML 文件。但是我完全无法让它工作,我现在真的很沮丧。我只是无法让它工作。
数据库是 Postgres 9.2,类是我自己编写的,Hibernate 版本是 Netbeans 7.3(我相信是 3.2)附带的。
表格如下:
CREATE TABLE sellable.manufacturers
(
mfr_id serial NOT NULL, -- Manufacturer ID
mfr_name character varying(127) NOT NULL, -- Manufacturer name
CONSTRAINT manufacturers_pkey PRIMARY KEY (mfr_id),
CONSTRAINT manufacturers_mfr_name_key UNIQUE (mfr_name)
);
我试图将其映射到的类如下:
package bikeshop.sellable;
import java.io.Serializable;
public class Manufacturer implements Serializable {
private Integer mfrId = null;
private String mfrName = null;
public Integer getMfrId () {
return this.mfrId;
}
public String getMfrName () {
return this.mfrName;
}
public void setMfrName (String MfrName) {
this.mfrName = MfrName;
}
}
Hibernate 映射的 XML 是:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 21, 2013 7:20:20 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="bikeshop.sellable.Manufacturer" table="manufacturers" schema="sellable">
<comment>Product manufacturers</comment>
<id name="mfrId" type="int">
<column name="mfr_id" />
<generator class="assigned" />
</id>
<property name="mfrName" type="string">
<column name="mfr_name" length="127" not-null="true" unique="true">
<comment>Manufacturer name</comment>
</column>
</property>
</class>
</hibernate-mapping>
Hibernate 项目配置是:
<?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.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/bikeshop</property>
<property name="hibernate.connection.username">bikeshop</property>
<property name="hibernate.connection.password">bikeshop</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.format_sql">true</property>
<mapping class="bikeshop.sellable.Manufacturer" file="" jar="" package="" resource="bikeshop/mappings/Manufacturers.hbm.xml"/>
<mapping resource="bikeshop/mappings/Sellables.hbm.xml"/>
</session-factory>
</hibernate-configuration>
当我尝试使用 HQL 查询编辑器时,我得到的只是显示的“select from”查询,显然无法执行。尝试执行它会导致异常。
org.hibernate.exception.SQLGrammarException:无法执行查询
为什么这无法生成有效的 SQL 查询?我错过了什么?
编辑:我一直试图让它工作,现在我遇到了一个不同的错误。HQL 查询窗口现在显示消息“无法从数据库中检索数据”。并且异常已更改为“org.hibernate.PropertyNotFoundException:在类bikeshop.sellable.Manufacturer 中找不到属性mfrId 的设置器”
配置文件已更改为:
<?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.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/bikeshop</property>
<property name="hibernate.connection.username">bikeshop</property>
<property name="hibernate.connection.password">bikeshop</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.format_sql">true</property>
<mapping class="bikeshop.sellable.Manufacturer" file="" jar="" package="bikeshop.sellable" resource="bikeshop/mappings/manufacturer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
并且映射文件已更改为:
<?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">
<hibernate-mapping>
<class name="bikeshop.sellable.Manufacturer" table="manufacturers" schema="sellable">
<comment>Product manufacturers</comment>
<id name="mfrId" type="int">
<column name="mfr_id" />
<generator class="native" />
</id>
<property name="mfrName" type="string">
<column name="mfr_name" length="127" not-null="true" unique="true">
<comment>Manufacturer name</comment>
</column>
</property>
</class>
</hibernate-mapping>
编辑 2:作为最后一次尝试,我将所有制造商成员公开,并将访问类型更改为 XML 中的字段。
现在 HQL 查询工作并返回结果。但我显然不想采取这种方法,公共领域是一个可怕的想法!