2013-08-21 188 views
1

我目前正在学习如何使用Hibernate将对象存储在数据库中。我一直在通过教程https://netbeans.org/kb/docs/java/hibernate-java-se.html,我正在尝试为一个非常简单的类和表创建一个XML文件。然而,我完全无法让它工作,我现在真的很沮丧。我只是无法让它工作。Hibernate初学者

的数据库是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查询编辑器,我得到的是查询显示,这显然不能执行“从选择”。试图执行它会导致异常。

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查询工作并返回结果。但我显然不想采取这种方式,公共领域是一个可怕的想法!

+0

查询或查询编辑器是否有问题? –

+0

我输入“从制造商”到编辑器,它生成的查询是“从中选择” – GordonM

+0

只是一个猜测,但也许你需要完全限定制造商与它的包名称。 –

回答

0

看来这个问题是HQL编辑器没有更新类或元数据时,您更改的类或它的元数据中的所有的时间,这也似乎这可不管你是否使用外部发生XML文件或内联注释。

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