今天調試程序的時候,發現插入數據時老是拋出異常:"a different object with the same identifier value was already associated with the session"
起初以為是override hashCode()的方法不對。后來google查了查多數問題都出現在detached對象之后,再鏈接session由于對象的改變而出現的異常。
最后發現原來問題很簡單....是在用annotation重寫mapping的時候,忘記給@Id加上@GeneratedValue,導致第一個row insert后,再次insert時沒有生成新的id!
看來annotation雖然方便了mapping,但由于和代碼集中在一塊了,很容易出現疏忽大意的錯誤,而且一般都不會注意到-.-
想起了python的SQLObject, SQLAlchemy,雖然功能沒有Hibernate豐富,但是簡單實用!
posted on 2007-03-29 20:28
hijeff 閱讀(407)
評論(0) 編輯 收藏 所屬分類:
Hibernate 、
ErrorLog