多対一の関係があり、値を挿入しようとすると、前のキーが渡されません。休止状態は、生成された SQL クエリにその値を含めません。
City および LocalizedLocation エンティティの定義:
<!-- LocalizedLocation -->
<hibernate-mapping>
<class name="servicedb.dal.domain.LocalizedLocation" table="localized_location" catalog="DB">
<composite-id name="id" class="servicedb.dal.domain.LocalizedLocationId">
<key-property name="localeId" type="int">
<column name="locale_id" />
</key-property>
<key-property name="locationId" type="int">
<column name="location_id" />
</key-property>
</composite-id>
<many-to-one name="location" class="servicedbcedb.dal.domain.Location" update="false" insert="false" fetch="select">
<column name="location_id" not-null="true" />
</many-to-one>
<many-to-one name="city" class="servicedb.dal.domain.City" update="false" insert="false" fetch="select" cascade="all">
<column name="locale_id" not-null="true" />
<column name="country_code" length="2" not-null="true" />
<column name="city_id" not-null="true" />
</many-to-one>
<property name="title" type="string">
<column name="title" length="120" />
</property>
</class>
</hibernate-mapping>
<!-- City -->
<hibernate-mapping>
<class name="servicedb.dal.domain.City" table="city" catalog="DB">
<composite-id name="id" class="servicedb.dal.domain.CityId">
<key-property name="localeId" type="int">
<column name="locale_id" />
</key-property>
<key-property name="countryCode" type="string">
<column name="country_code" length="2" />
</key-property>
<key-property name="id" type="int">
<column name="id" />
</key-property>
</composite-id>
<property name="name" type="string">
<column name="name" length="100" not-null="true" />
</property>
<set name="localizedLocations" table="localized_location" inverse="true" lazy="true" fetch="select">
<key>
<column name="locale_id" not-null="true" />
<column name="country_code" length="2" not-null="true" />
<column name="city_id" not-null="true" />
</key>
<one-to-many class="servicedb.dal.domain.LocalizedLocation" />
</set>
</class>
</hibernate-mapping>
次のコードは、場所を挿入してから LocalizedLocation を挿入する必要があります。LocalizedLocation には、挿入された場所を指す外部キーが必要ですが、何らかの理由でそうではありません。
Session session = locationDAO.getSession();
session.beginTransaction();
// Location inititalization, the object is correctly populated
session.save(location);
LocalizedLocation localizedLocation = new LocalizedLocation();
localizedLocation.setId(new LocalizedLocationId(locale.getId(), location.getId()));
localizedLocation.setCity(city); // the city already exists on the database, object is not null
localizedLocation.setLocale(locale); // the locale already exusts on the database
localizedLocation.setLocation(location);
session.save(localizedLocation);
session.getTransaction().commit();
コミット後、生成された挿入クエリは次のようになります。
insert into DB.localized_location (title, description, locale_id, location_id) values (?, ?, ?, ?)
ただし、次のようにする必要があります。
insert into DB.localized_location (title, description, locale_id, location_id, city_id, country_code) values (?, ?, ?, ?, ?, ?)
市テーブルへの外部キーが生成された sql 挿入ステートメントに含まれていない理由を知っている人はいますか?
また、Eclipse と reveng.xml を使用してデータベースをリバース エンジニアリングしているため、hbm ファイルは自動生成され、EJB3 アノテーションは使用していません。