org.hibernate.PersistentObjectException: detached entity passed to persist

后端 未结 7 1413
生来不讨喜
生来不讨喜 2020-11-30 00:56

I had successfully written my first master child example with hibernate. After few days I took it again and upgraded some libraries. No sure what did I do but I could never

7条回答
  •  时光说笑
    2020-11-30 01:40

    Most likely the problem lies outside the code you are showing us here. You are trying to update an object that is not associated with the current session. If it is not the Invoice, then maybe it is an InvoiceItem that has already been persisted, obtained from the db, kept alive in some sort of session and then you try to persist it on a new session. This is not possible. As a general rule, never keep your persisted objects alive across sessions.

    The solution will ie in obtaining the whole object graph from the same session you are trying to persist it with. In a web environment this would mean:

    • Obtain the session
    • Fetch the objects you need to update or add associations to. Preferabley by their primary key
    • Alter what is needed
    • Save/update/evict/delete what you want
    • Close/commit your session/transaction

    If you keep having issues post some of the code that is calling your service.

提交回复
热议问题