使用jpa remove删除对象

时间:2017-06-19 19:39:54

标签: jpa

请有人帮助我。

ManagedBean

public Customer deleteCustomer(Customer customer){
    BookstoreManager.getInstance().deleteCustomer(customer);
    System.out.println("Customer deleted" + customer.getFirstName());
    return  null;
}

Business Logik

public Customer deleteCustomer(Customer deletedCustomer) {
    try {
        em.getTransaction().begin();
        Customer customer = new Customer();
        em.remove(customer);
        em.getTransaction().commit();
    } catch (Exception e) {
        em.getTransaction().rollback();
        e.printStackTrace();
        System.out.println("Hello from delete");
    }
    return null;
}

错误消息

  

信息:HHH000114:处理删除处理中的瞬态实体

1 个答案:

答案 0 :(得分:0)

您的意思是em.remove(deletedCustomer);吗?

public Customer deleteCustomer(Customer deletedCustomer) {
    try {
        em.getTransaction().begin();
        em.remove(deletedCustomer);
        em.getTransaction().commit();
    } catch (Exception e) {
        em.getTransaction().rollback();
        e.printStackTrace();
        System.out.println("Hello from delete");
    }
    return null;
}

您只能删除状态为managed的实体。 enter image description here

来源http://www.objectdb.com/java/jpa/persistence/managed