我想保存一個具有集合的實體作爲也必須在數據庫中保存的屬性,所以我遇到以下問題:如果在保存集合之前保存集合,我得到了以下錯誤:用Spring在Hibernate中保存集合
object references an unsaved transient instance - save the transient instance before flushing: asd.qwe.MyCollection; nested exception is org.hibernate.TransientObjectException: ...
否則,如果我嘗試保存我的實體之前保存收藏,或者如果我在list
定義設置cascade="save-update"
,我會得到這樣的:
Hibernate operation: could not insert: [asd.qwe.MyCollection]; uncategorized SQLException for SQL [insert into entity_collections (fk_entity, field2, field3) values (?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'fk_entity' doesn't have a default value; nested exception is java.sql.SQLException: Field 'fk_entity' doesn't have a default value
這完全正確,因爲'fk_entity'是指我的實體尚未保存,並且具有NOT NULL限制。
我想知道爲什麼Hibernate試圖在實際的實體之前保存集合?而..是否有可能扭轉儲蓄秩序?
你能分享你的映射嗎?這聽起來像是有些不對勁。如果映射正確,Hibernate會按照您正確指出的順序對插入進行排序以避免外鍵違例。 – 2011-12-19 10:06:15