IllegalArgumentException: Entity must be managed to call remove

房东的猫 提交于 2019-12-14 03:15:35

问题


Controller page has following code

                BigDecimal id=new BigDecimal(request.getParameter("empId"));
                employee.setEmpno(id);
                flag = factory.removeEmployee(employee);

TransactionFactory class

    public final class EntityTransactionFactory implements TransactionFactory{

    @PersistenceContext
    private EntityManagerFactory Factory;
    private EntityManager Manager;
    @Resource
    private UserTransaction Transaction;
    private static final TransactionFactory transaction = new EntityTransactionFactory();

    private EntityTransactionFactory() {

    }

    public static TransactionFactory getInstance() {
        return transaction;
    }


    /**
     * @return the Factory
     */
    @Override
    public EntityManagerFactory getFactory() {
        Factory= Persistence.createEntityManagerFactory("SampleBeanPU");
        return Factory;
    }

    /**
     * @return the Manager
     */
    @Override
    public EntityManager getManager() {
        Manager=getFactory().createEntityManager();
        return Manager;
    }

    /**
     * @return the Transaction
     */
    @Override
    public UserTransaction getTransaction() {
        try {
            Transaction = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
        } catch (NamingException e) {
            Transaction=null;
        }
        return Transaction;
    }
}

emp variable name of Emp type.

        TransactionFactory factory=EntityTransactionFactory.getInstance();
        factory.getTransaction().begin();
        factory.getManager().joinTransaction();

        Emp ref= factory.getManager().getReference(Emp.class, emp.getEmpno());
        System.out.println(ref.getEname());
        factory.getManager().remove(factory.getManager().merge(ref));
        System.out.println(factory.getTransaction().getStatus());
        factory.getTransaction().commit();
        System.out.println(factory.getTransaction().getStatus());
        //factory.getManager().flush();
        factory.getManager().close();
        factory.getFactory().close();

But, getting following exception, which is pointing on above code.

 java.lang.IllegalArgumentException: Entity must be managed to call remove: com.entity.Emp[ empno=1234 ], try merging the detached and try the remove again.

回答1:


As suggested by @JBNizet

I'm creating instance of EntityManagerFactory only once by putting into private constructor and retrieving instance by getter method of this class.

And for removing entity I used following code

        Emp ref = factory.getManager().find(Emp.class, emp.getEmpno());           
        factory.getManager().remove(ref);

And, It works.!!



来源:https://stackoverflow.com/questions/22602923/illegalargumentexception-entity-must-be-managed-to-call-remove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!