多対一の関係があり、値を挿入しようとすると、前のキーが渡されません。
エンティティの国
<hibernate-mapping>
<class name="servicedb.dal.domain.Country" table="Country" catalog="DB">
<composite-id name="id" class="servicedb.dal.domain.CountryId">
<key-property name="countryCode" type="string">
<column name="countryCode" length="2" />
</key-property>
<key-property name="localeId" type="int">
<column name="localeId" />
</key-property>
</composite-id>
<many-to-one name="locale" class="servicedb.dal.domain.Locale" update="false" insert="false" fetch="select">
<column name="localeId" not-null="true" />
</many-to-one>
<property name="name" type="string">
<column name="name" length="60" not-null="true" />
</property>
<set name="cities" table="City" inverse="true" lazy="true" fetch="select">
<key>
<column name="countryCode" length="2" not-null="true" />
<column name="localeId" not-null="true" />
</key>
<one-to-many class="servicedb.dal.domain.City" />
</set>
</class>
エンティティ市
<hibernate-mapping>
<class name="servicedb.dal.domain.City" table="City" catalog="DB">
<composite-id name="id" class="servicedb.dal.domain.CityId">
<key-property name="id" type="int">
<column name="id" />
</key-property>
<key-property name="localeId" type="int">
<column name="localeId" />
</key-property>
</composite-id>
<many-to-one name="country" class="servicedb.dal.domain.Country" update="false" insert="false" fetch="select">
<column name="countryCode" length="2" not-null="true" />
<column name="localeId" not-null="true" />
</many-to-one>
<property name="name" type="string">
<column name="name" length="100" not-null="true" />
</property>
<set name="localizedLocations" table="LocalizedLocation" inverse="true" lazy="true" fetch="select">
<key>
<column name="cityId" />
<column name="localeId" not-null="true" />
</key>
<one-to-many class="servicedb.dal.domain.LocalizedLocation" />
</set>
</class>
</hibernate-mapping>
City エンティティを作成するときに、Country を正しく設定していますが、countryCode は null ではありません。ただし、生成されたクエリは次のようになります。
insert into Db.City (name, id, localeId) values (?, ?, ?)
ただし、次のようにする必要があります。
insert into Db.City (name, id, localeId, countryCode) values (?, ?, ?, ?)
そして休止状態は次の例外をスローします
org.hibernate.exception.GenericJDBCException: Field 'countryCode' doesn't have a default value
提供された情報がエラーの原因を理解するのに十分であることを願っています。そうでない場合は、具体的に追加情報を求めてください。
また、Eclipse と reveng.xml を使用してデータベースをリバース エンジニアリングしているため、hbmファイルは自動生成され、EJB3 アノテーションは使用していません。
編集: Country および City エンティティの完全なマッピングを投稿しました。