解决object references an unsaved transient instance - save the transient instance before flushing 的错误
生活随笔
收集整理的这篇文章主要介绍了
解决object references an unsaved transient instance - save the transient instance before flushing 的错误
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
异常信息:
Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing :
错误原因:
这个异常是在ManyToOne级联操作时遇到,要保存的对象引用了一个未保存的对象,比如我们试图存储一个瞬时状态的对象。也就是说,A和B有关联的话,其中A引用B。如果我们要持久化A对象的话,应该先将其关联的B对象先持久化才可以,否则就会出现这个异常。
解决方法:
解决办法是将ManyToOne的级联设为:cascade = CascadeType.PERSIST
延伸:
- PERSIST:持久保存拥有方实体时,也会持久保存该实体的所有相关数据。
- MERGE:将分离的实体重新合并到活动的持久性上下文时,也会合并该实体的所有相关数据。
- REMOVE:删除一个实体时,也会删除该实体的所有相关数据。
- ALL:以上都适用。
总结
以上是生活随笔为你收集整理的解决object references an unsaved transient instance - save the transient instance before flushing 的错误的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: piranha启动报错
- 下一篇: 2022图像翻译/扩散模型:UNIT-D