Hello everyone.

When I try to delete a register of the database there is a HTTP Status
500 screen with the next exception:

javax.servlet.ServletException: java.lang.IllegalArgumentException: An
instance of a null PK has been incorrectly provided for this find
operation.


I have developed 4 activities to this homework:

*****FIRST ACTIVITY*****

I modified ListPerson.jsp with the next instruction to add a column
with a link to delete each one of the registers of the database.

<a href="DeletePersonServlet"><strong>Delete</strong></a>


*****SECOND ACTIVITY*****

The DeletePersonServlet code is:

public class DeletePersonServlet extends HttpServlet {

    @PersistenceUnit
    //The emf corresponding to
    private EntityManagerFactory emf;
    @Resource
    private UserTransaction utx;

    protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
            throws ServletException, IOException {
        assert emf != null;  //Make sure injection went through
correctly.

        EntityManager em = null;

        try {
            //Get the data from user's form
            String id = (String) request.getParameter("id");
            //create an em.
            em = emf.createEntityManager();
            //begin a transaction
            utx.begin();

            // read a person instance
            Person person = em.find(Person.class, id);

            // delete the register
            em.remove(person);

            //commit transaction
            utx.commit();

            //Forward to ListPerson to list persons along with the
newly
            //removed person above
            request.getRequestDispatcher("ListPerson").forward
(request, response);
        } catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            //close the em to release any resources held up by the
persistence provider
                em.close();
        }
    }


*****THIRD ACTIVITY*****

I looked for information on the Internet and I found 2 documents of
ORACLE with interesting information about this:

In this link 
http://www.oracle.com/technology/products/ias/toplink/jpa/tutorials/jsf-jpa-tutorial.html
, you can see:
Create an Entity
Read an Entity
Update an Entity
Delete an Entity

In this link 
http://www.oracle.com/technology/products/ias/toplink/jpa/tutorials/jsf-jpa-tutorial.html
, you can see:
Persisting a new Entity
Modifying an existing Entity
Deleting an Entity


*****FOURTH ACTIVITY*****
I suppose that the exception (null pk) is because “id” is a String,
and the code look for a number.
I have tried to adapt the next code to the homework but I couldn’t
yet.



READ AN ENTITY BY PRIMARY KEY

public Order getOrderById(long orderId){
    EntityManager em = jpaResourceBean.getEMF().createEntityManager();
    try{
        return em.find(Order.class, orderId);
    }finally{
        em.close();
    }
}


DELETE AN ENTITY:

public void requestCancelOrder(long orderId){
    EntityManager em = jpaResourceBean.getEMF().createEntityManager();
    try{
        em.getTransaction().begin();
        Order order = em.find(Order.class, orderId);
        em.remove(order);
        em.getTransaction().commit();
    }finally{
        em.close();
    }
}


I have tested to adapt the code to Read an Entity by primary key and
after delete the register.
I tried to put this code in Person.java, in DeletePersonServlet, or
create other servlet to this, but I couldn’t yet.


I feel enough confusion about how to adapt the code to read an entity
by primary key, to the homework. I don’t know if this is the correct
way to solve this homework or there is other option.

Can someone advice me about possible solutions of this homework,
please?

Thanks in advance
Cruz

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Java 
EE (J2EE) Programming with Passion!" group.
To post to this group, send email to 
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to 
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to