hibernateとJPAの新機能であり、注釈に問題があります。
私の目標は、dbにこのテーブルを作成することです(personal-detailsを含むPERSON_TABLE)
ID ADDRESS NAME SURNAME MUNICIPALITY_ID
まず第一に、私は私の国のすべての自治体を含むdbにMUNICIPALITYテーブルを持っています。このテーブルをこのエンティティにマッピングしました:
@Entity
public class Municipality implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String country;
private String province;
private String name;
@Column(name="cod_catasto")
private String codCatastale;
private String cap;
public Municipality() {
}
...
次に、単純なアドレスを実現するフィールドを含むEMBEDDABLEクラスのアドレスを作成します。
@Embeddable
public class Address implements Serializable {
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_municipality")
private Municipality municipality;
@Column(length=45)
private String address;
public Address() {
}
...
最後に、このクラスをPersonENTITYに埋め込みました
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
@Embedded
private Address address;
public Person() {
}
...
新しいPersonレコードを保存する必要がある場合は、すべて正常に機能します。実際、hibernateは必要に応じてPERSON_TABLEを作成しますが、Personレコードを取得しようとすると、例外が発生します。HQLは単に「Personから」です。例外は次のとおりです(Entitiesは上記のすべてのクラスを含むパッケージです)。
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on Entities.Person.address.municipality references an unknown entity: Entities.Municipality
@OneToOneアノテーションは問題ですか?
私のhibernate.cfg.xmlは次のとおりです。
<?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.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:tcp://localhost/DB_PATH</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="Entities.Person"/>
<mapping class="Entities.Municipality"/>
<mapping class="Entities.Address"/>
</session-factory>
</hibernate-configuration>
ありがとう。