Is there a way to pass detached object to JPA persist? (detached entity passed to persist)

前端 未结 3 2042
梦如初夏
梦如初夏 2020-12-28 21:45

I have 2 entities : Account and AccountRole.

public class Account {
   private AccountRole accountRole;

   @ManyToOne(cascade = Ca         


        
3条回答
  •  余生分开走
    2020-12-28 22:07

    It looks like you leave the transaction during your processing, so the accountRole gets detached, or it is already detached for other reasons.

    A call to entityManager.merge(accountRole) before calling entityManager.persist(account) should fix it.

    EDIT: Unfortunately, if you cannot be sure if the accountRole already exists in the DB, you will have to check it by querying. If it exists - merge, if not - persist. It is indeed a hassle, but I have not yet seen a better workaround.

    EDIT2: The entity you pass to the merge method will remain detached - the managed entity will be returned by the merge, so you would need to merge first, then set the reference on the account to the return value of the merge.

提交回复
热议问题