我目前正在学习如何使用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的setter“。
配置文件已更改为:
<?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查询工作并返回结果。但我显然不想采取这种方法,公共领域是一个可怕的想法!
发布于 2013-08-25 11:24:03
问题似乎在于,在对类或其元数据进行更改时,HQL编辑器并不总是得到更新的类或元数据,不管您是使用外部XML文件还是内联注释,都可能发生这种情况。
到目前为止,我找到的唯一解决办法是退出Netbeans并重新启动它。这使得HQL查询编辑器更新,但显然很烦人。
https://stackoverflow.com/questions/18365222
复制相似问题