休眠没有设置插入外键
我有多对一的关系,当我试图插入一个值时,外键没有通过。休眠不包括它在生成的SQL查询中的值。市和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查询如下:
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注释。
您的城市 LocalizedLocation映射中的属性似乎具有“insert = false”和“update = false”。按照这种方式进行映射,这使得它成为“反向”关系,其中连接仅从另一侧保存 - 即保存位置。
要么删除这两个属性,要么在建立关系后保存城市。
编辑:更正了属性。
顺便说一句,如果城市没有映射LocalizedLocations列表 - 因为我以某种方式从您的描述中得到了这个想法 - 似乎没有意义,因为插入该关系的唯一方法是将这两个属性设置为false将映射同一列上的另一个属性(或直接在db上执行)。
我不确定我是否了解您的建议。 location_id发布正确,缺少的值是来自localized_location的city_id和country_code。 – aumanets 2013-03-17 22:17:31
对不起,我错位的属性名称,但相同的概念适用于_city_,因为你有“更新”和“创造”在*假*那里。为了清楚起见,我会更新答案,但建议几乎是一样的,只是在“城市”关系上。 – Grim 2013-03-17 22:24:05
我尝试了两种方法,在建立关系后删除插入和更新属性并保存城市。但外键仍然缺失。 – aumanets 2013-03-17 22:40:57
您是否曾经找到过解决方案? – 2013-12-27 19:17:08
帮助@JoshC。我试图通过多个表属性来重用locale_id。我不知道为什么Hibernate不喜欢它,但出于某种原因,它没有在插入时填充FK。为了解决这个问题,我为LocaleId创建了一个附加属性。因此,locale_id仍然是位置表的PK,而附加属性locale_city_id成为城市表的FK。两个属性始终具有相同的值(这由DAL逻辑保证)。通过所描述的方法,您将在数据库中获得冗余信息,但我不知道是否有更好的解决方案。 – aumanets 2013-12-30 14:41:35