JPA thinks I'm deleting a detached object

前端 未结 7 2213
萌比男神i
萌比男神i 2020-12-14 00:30

I\'ve got a DAO that I used to load and save my domain objects using JPA. I finally managed to get the transaction stuff working, now I\'ve got another issue.

In my

7条回答
  •  臣服心动
    2020-12-14 01:06

    Here is what I used (based on the previous answers)

    public void deleteTask(int taskId) {
        Task task = getTask(taskId); //this is a function that returns a task by id
        if (task == null) {
            return;
        }
        EntityManager em = emf.createEntityManager();
        EntityTransaction et = em.getTransaction();
        et.begin();
        em.remove(em.merge(task));
        et.commit();
        em.close();
    }
    

提交回复
热议问题