我需要升级bd中多个记录的字段。如何使用JPA做到这一点?我尝试了以下操作,但不起作用:

@Override
public String estadoPedido(List<DetallePedido> lista) {
    int cod;
    String mensage = null;
    for (DetallePedido ped : lista) {
        cod = ped.getIdDetalle();
        EntityManager em = emf.createEntityManager();
        DetallePedido detPed = new DetallePedido();

        try {
            em.getTransaction().begin();
            detPed = em.find(DetallePedido.class, cod);
            detPed.setPedEstado("EN PLAN");
            em.merge(detPed);
            em.getTransaction().commit();
            mensage = "detalle seleccionado";
        } catch (Exception e) {
            mensage = "Error:/p" + e.getMessage();
        } finally{
            em.close();
        }
    }

    return mensage;
}

最佳答案

请尝试以下操作:

@Override
public String estadoPedido(List<DetallePedido> lista) {
    EntityManager em = emf.createEntityManager();
    //em.getTransaction().begin(); //Consider using Container managed transactions ,
    // if you do remove this line and the line above, and have
    //entity manager injected !
    String mensage = null;
    try {
         for (DetallePedido ped : lista) {
            detPed.setPedEstado("EN PLAN");
            em.merge(detPed);
         }
         mensage = "detalle seleccionado";
         em.getTransaction().commit; //consider removing this line and use Container managed transactions!
    } catch (Exception e)  {
         mensage = "Error:/p" + e.getMessage();
    } finally{
       em.close();
    }
    return mensage;
}

关于java - 使用jpa多次更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30882151/

10-13 05:01