如何解决嵌套异常是 org.hibernate.PersistentObjectException JPA Spring Boot
您好,我正在尝试将一行保存到我的数据库中,我已经花了一整天的时间来保存! 这是我的实体
订单详情实体:https://pastebin.com/HAGVUVEF
类别实体:https://pastebin.com/AWvbFKV0
当我尝试在 CheckoutServive 中将 OrderDetails 列表保存到数据库时,出现以下错误
"feedback": [
//Before mutating the body
{
"firstname": "Anis","lastname": "Sina","phone": "00000000","email": "anis@gmail.com","message": "First Feedback","id": 1
},//After mutating the body
{
"firstname": "Daniel","lastname": "Scout","email": "daniel@gmail.com","message": "Second Feedback","image": "feedback_images/(undefined).jpg","id": 2
},]
好吧,我正在尝试通过这种方法保存 OrderDetails 列表
2021-04-02 01:15:03.961 ERROR 9076 --- [o-8080-exec-111] o.s.b.w.servlet.support.ErrorPageFilter :
Forwarding to error page from request [/user/checkout/process] due to exception [detached entity
passed to persist: coffeeshop.entity.Category; nested exception is
org.hibernate.PersistentObjectException: detached entity passed to persist:
coffeeshop.entity.Category]
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist:
coffeeshop.entity.Category; nested exception is org.hibernate.PersistentObjectException: detached
entity passed to persist: coffeeshop.entity.Category
请注意,当我在购物车的控制器上使用相同的方法时,它可以完美运行, 在这种情况下,我得到了上面的错误。 附注。如果我的 OrderDetails 对象在他的类别列表中没有任何类别,那就没问题,保存成功
解决方法
从订单详细信息中删除类别属性中的 (cascade = CascadeType.ALL)。它试图在数据库中持久化一个已经持久化(所以它是分离的)的类别。您不想重新创建类别,因为它在其他实体之间共享。 Cascade ALL 还包含 PERSIST,它将再次尝试保存类别。
已修正 here,谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。