Hibernate初学者

时间:2013-08-21 18:41:04

标签: java hibernate postgresql netbeans

我目前正在尝试使用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查询起作用并返回结果。但我显然不想采取这种做法,公共领域是一个可怕的想法!

1 个答案:

答案 0 :(得分:0)

似乎问题是当您对类或其元数据进行更改时,HQL编辑器不会一直获得更新的类或元数据,无论您是否使用外部XML,似乎都会发生这种情况文件或内联注释。

到目前为止,我发现的唯一解决方法是退出Netbeans并重新启动它。这使得HQL查询编辑器更新,但显然非常烦人。