一、異常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:
這主要是在ManyToOne級聯操作時遇到,比如new了一個新對象,在未保存之前將它保存進了一個新new的對象(也即不是持久態)。
解決辦法是在保存或更新之前把這個對象查出來(這樣就是一個持久態)。
解決辦法是將many-to-one的級聯設為: cascade="save-update,persist"
四種 CASCADE 類型:
* PERSIST:持久保存擁有方實體時,也會持久保存該實體的所有相關數據。
* MERGE:將分離的實體重新合並到活動的持久性上下文時,也會合並該實體的所有相關數據。
* REMOVE:刪除一個實體時,也會刪除該實體的所有相關數據。
* ALL:以上都適用。
二、異常1:not-null property references a null or transient value
解決方法:將“一對多”關系中的“一”方,not-null設置為false
異常2:org.hibernate.TransientObjectException: object references an unsaved transient instance
解決方法:cascade="save-update,persist"
異常3:org.hibernate.QueryException: could not resolve property
解決方法:"from Category category where category.userID = :userID"修改為"from Category category where userID = :userID"或者"from Category category where category.user.id = :userID"
異常4:could not initialize proxy - the owning Session was closed
解決方法:設置lazy為false