我有多對一的關係,當我試圖插入一個值時,外鍵不通過。休眠不插入外鍵
Enitnty國家
<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>
當我創建了城市的實體,我在正確設置國家和COUNTRYCODE不爲空。但是生成的查詢如下所示:
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註釋。
編輯:張貼國家和城市實體的完整映射。
您可以發佈國家和城市的整個映射嗎? – overmeulen 2013-03-15 11:30:25
@overmeulen我已經添加了國家和城市的整個映射。 – aumanets 2013-03-15 11:42:58
看到這裏http://stackoverflow.com/questions/804514/hibernate-field-id-doesnt-have-a-default-value – PSR 2013-03-15 11:45:47