Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 冬眠初学者_Java_Hibernate_Postgresql_Netbeans - Fatal编程技术网

Java 冬眠初学者

Java 冬眠初学者,java,hibernate,postgresql,netbeans,Java,Hibernate,Postgresql,Netbeans,我目前正在尝试学习使用Hibernate在数据库中存储对象。我已经完成了在的一个教程,我正在尝试为一个非常简单的类和表创建一个XML文件。然而,我完全无法让它工作,我现在真的很沮丧。我就是不能让它工作 数据库是Postgres9.2,类是我自己编写的,Hibernate版本是NetBeans7.3(我相信是3.2)附带的版本 下表如下: CREATE TABLE sellable.manufacturers ( mfr_id serial NOT NULL, -- Manufacturer

我目前正在尝试学习使用Hibernate在数据库中存储对象。我已经完成了在的一个教程,我正在尝试为一个非常简单的类和表创建一个XML文件。然而,我完全无法让它工作,我现在真的很沮丧。我就是不能让它工作

数据库是Postgres9.2,类是我自己编写的,Hibernate版本是NetBeans7.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;
    }
}
我试图将其映射到的类如下所示:

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>

org.hibernate.dialogue.PostgreSqlDialogue
org.postgresql.Driver
jdbc:postgresql://localhost/bikeshop
自行车店
自行车店
真的
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
真的
当我尝试使用HQL查询编辑器时,得到的只是显示的查询“select from”,显然无法执行。尝试执行它会导致异常

org.hibernate.exception.sqlgrammareexception:无法执行查询

为什么无法生成有效的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>

org.hibernate.dialogue.PostgreSqlDialogue
org.postgresql.Driver
jdbc:postgresql://localhost/bikeshop
自行车店
自行车店
真的
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
真的
并且映射文件已更改为:

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

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


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

您遇到的问题是查询还是查询编辑器?我在编辑器中键入“from Manufacturer”,它生成的查询是“select from”,只是一个猜测,但您可能需要使用其包名完全限定制造商。我一直在瞎弄它,现在我得到了一个关于缺少setter的不同错误。我为制造商ID设置了一个setter,尽管它应该由DB设置,并且不可编辑,但是如果我将类字段公开,并将XML更改为使用字段而不是getter/setter,那么我仍然会得到setter错误。它可以工作并成功执行查询。然而,我显然不想要公共字段,这绝对是我最不想做的事情