나는 현재 Hibernate를 사용하여 데이터베이스에 객체를 저장하는 방법을 배우려고하고있다. 나는 https://netbeans.org/kb/docs/java/hibernate-java-se.html에서 튜토리얼을 통해 작업 해왔고 매우 간단한 클래스와 테이블을위한 XML 파일을 만들려고합니다. 그러나 나는 그것을 완전히 작동시킬 수 없다. 그리고 나는 정말로 좌절하고있다. 나는 그걸 작동시킬 수 없다.Hibernate beginner
데이터베이스는 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)
);
클래스 I는 다음대로에 매핑하기 위해 노력하고있어 : 하이버 네이트 매핑에 대한 XML은
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;
}
}
됩니다
:<?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 쿼리 창에 "데이터베이스에서 데이터를 검색 할 수 없습니다."라는 메시지가 표시됩니다. 그리고 예외가 변경되었습니다
<?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.PropertyNotFoundException 클래스 bikeshop.sellable.Manufacturer의 특성 mfrId에 대한 세터를 찾을 수 없습니다"
<?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 쿼리가 작동하고 결과를 반환합니다. 하지만 분명히 그런 접근 방식을 사용하고 싶지는 않습니다. 공개 필드는 끔찍한 생각입니다!
문제가있는 쿼리 또는 쿼리 편집기입니까? –
"제조 업체"를 편집기에 입력하면 생성되는 쿼리는 "선택" – GordonM
일뿐입니다.하지만 아마도 패키지 이름으로 제조업체를 정규화해야합니다. –