我需要为某个学生存储实验室分数,第一个 Select 效果很好,但是当我尝试使用 WHERE 子句更新 lab_points 的新值时,程序崩溃了。当我跳过 WHERE 程序运行良好但所有值都已更改时。
UPDATE 会有什么问题?
EntityManagerFactory emf = (EntityManagerFactory)getServletContext().getAttribute("emf");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
EntityManager updateEm = emf.createEntityManager();
String nume, prenume;
int lab_points;
for(List<String> linie: linesAsList)
if(linie.get(1).matches("[a-zA-Z- ]+") && linie.get(2).length()>=2) {
nume = linie.get(1).split(" ")[0];
prenume = linie.get(1).split(" ")[1];
lab_points = (Integer) em.createQuery("select n.lab_points from Record as n where n.student.nume=:nume and n.student.prenume=:prenume and n.student.grupa=:grupa and n.cours.numeCurs=:curs")
.setParameter("nume", nume).setParameter("prenume", prenume)
.setParameter("grupa",linie.get(2)).setParameter("curs","Programare avansata").getSingleResult();
lab_points = lab_points + Integer.parseInt(linie.get(3).trim());
System.out.println(lab_points);
updateEm.getTransaction().begin();
updateEm.createQuery("update Record as n set n.lab_points=:points where n.student.nume=:nume").setParameter("nume",nume)
.setParameter("points",lab_points)
.executeUpdate();
updateEm.getTransaction().commit();
System.out.println(em.createQuery("select n.lab_points from Record as n where n.student.nume=:nume and n.student.prenume=:prenume and n.student.grupa=:grupa and n.cours.numeCurs=:curs")
.setParameter("nume", nume).setParameter("prenume", prenume)
.setParameter("grupa", linie.get(2)).setParameter("curs", "Programare avansata").getSingleResult());
}
if (updateEm.getTransaction().isActive())
updateEm.getTransaction().rollback();
updateEm.close();
if (em.getTransaction().isActive())
em.getTransaction().rollback();
em.close();
这是我的数据库错误消息:错误