使用jpa进行多次更新

时间:2015-06-17 04:06:48

标签: java jpa

我需要在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;
}

1 个答案:

答案 0 :(得分:0)

请尝试以下方法:

template <class T>
class Vec : public std::vector<T>
{
public:
    Vec()
    {
        this->reserve(32000);
    }

    Vec(std::string s, int upperbound, int lowerbound)
    {
        SetBounds(s, upperbound, lowerbound);
    }

    void SetBounds(const std::string& s, int upperbound, int lowerbound)
    {
        mys = s;
        ub = upperbound;
        lb = lowerbound;

        std::cout << "resizing Vec for symbol: "
                  << symbol     << " "
                  << upperbound << " "
                  << lowerbound << " "
                  << upperbound - lowerbound << '\n';


        try
        {
           this->resize(ub - lb + 1);
        }
        catch(std::exception& ex)
        {
            std::cout << "Resize Exception: " << ex.what() << "\n";
        }
        catch(...)
        {
            std::cout << "SetBounds exception" << "\n";
        }
    }
public:
    T& operator[] (int idx)
    {
        try
        {
            //std::cout << idx << std::endl;

            return this->at(idx - lb);
        }
        catch(std::exception& ex)
        {
           std::cout << "Access Exception: " << idx << " " << symbol << " " << ex.what() << '\n';
        }
        catch(...)
        {
            std::cout << "Access Exception: " << idx << " "  << symbol << '\n';
        }
    }

private:
    std::string mys;
    int ub;
    int lb;
};