我有多对一的关系,当我尝试插入一个值时,不会传递foreigh键。 hibernate在生成的SQL查询中不包含它的值。
城市和本地化实体的定义:
<!-- 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>
以下代码应该插入Location,然后是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 insert语句中没有包含city表的外键吗?
我也使用eclipse和reveng.xml对数据库进行逆向工程,因此我的hbm文件是自动生成的,我没有使用EJB3注释。
答案 0 :(得分:0)
LocalizedLocation映射中的 city 属性似乎具有“insert = false”和“update = false”。以这种方式映射,它使这成为一种“反向”关系,其中连接仅从另一侧保存 - 即在保存位置时。
删除这两个属性,或者在设置关系后保存城市。
编辑:更正了该属性。
顺便说一下,如果城市没有映射LocalizedLocations列表 - 因为我从你的描述中得到了这个想法 - 将这两个属性设置为false似乎没有意义,因为插入该关系的唯一方法是在同一列上映射另一个属性(或直接在db上执行)。