简体   繁体   中英

org.hibernate.PersistentObjectException: detached entity passed to persist - ManyToMany Mapping

  • I have entity Patient and entity Test.
  • Both have ManyToMany relationships each other.
  • I added CASCADE_TYPE=ALL at both sides.
  • I am using Hibernate EntityManager's persist(Patient); method.
  • When i execute it it is throwing "org.hibernate.PersistentObjectException: detached entity passed to persist: Test"
  • These are the screens.

The Patient class 患者分类-映射

The Test class 测试类别-对应

If anyone have solution please share it.

Thanks.

thanks. i changed it to em.merge() from em.persist(). works fine.

merge and persist do pretty different things with your entity. Are you really sure that merge accomplishes what you intended to do in the first place ? (see this topic over here )

Without seeing the code that you wrote to persist the test - entity, I can't really tell you what went wrong, but I could imagine it having something to do with the manyToMany-relationships that you have there. Are you sure you want to cascade.ALL ? If you remove a test -instance from the database, the cascade will remove every patient in that list. And then, for every patient in that list, it will cascade.REMOVE every test in the List<test> .

我建议尝试修改CASCADE_TYPE=PERSIST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM