今天調(diào)試程序的時候,發(fā)現(xiàn)插入數(shù)據(jù)時老是拋出異常:"a different object with the same identifier value was already associated with the session"
起初以為是override hashCode()的方法不對。后來google查了查多數(shù)問題都出現(xiàn)在detached對象之后,再鏈接session由于對象的改變而出現(xiàn)的異常。
最后發(fā)現(xiàn)原來問題很簡單....是在用annotation重寫mapping的時候,忘記給@Id加上@GeneratedValue,導(dǎo)致第一個row insert后,再次insert時沒有生成新的id!
看來annotation雖然方便了mapping,但由于和代碼集中在一塊了,很容易出現(xiàn)疏忽大意的錯誤,而且一般都不會注意到-.-
想起了python的SQLObject, SQLAlchemy,雖然功能沒有Hibernate豐富,但是簡單實用!