简体   繁体   中英

IllegalArgumentException: Entity must be managed to call remove

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.

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.!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM