当我使用tx.commit一次以上时,出现此错误
报告类型例外
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /JSP/Upload /upload.jsp at line 444
441: Query qLVa = hibernateSessiona1.createQuery("from Livrea where NomLivre = :userLV ");
442: qLVa.setParameter("userLV", newName);
443:
444: Livrea LVa =(Livrea) qLVa.uniqueResult();
445:
446: Ajouter add = new Ajouter();
447:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:550)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:332)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
cause mère
javax.servlet.ServletException: net.sf.hibernate.NonUniqueResultException: query did not return a unique result: 2
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:901)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:830)
org.apache.jsp.JSP.Upload.upload_jsp._jspService(upload_jsp.java:612)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:332)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
cause mère
net.sf.hibernate.NonUniqueResultException: query did not return a unique result: 2
net.sf.hibernate.impl.AbstractQueryImpl.uniqueElement(AbstractQueryImpl.java:559)
net.sf.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:550)
org.apache.jsp.JSP.Upload.upload_jsp._jspService(upload_jsp.java:515)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:332)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/7
这是我的代码:
Session hibernateSession = HibernateUtil.currentSession();
Transaction tx = hibernateSession.beginTransaction();
Livrea LV = new Livrea();
LV.setNomLivre(newName);
hibernateSession.save(LV);
tx.commit();
HibernateUtil.closeSession();
if(session.getAttribute("type").equals("Enseignant")){
Session hibernateSessione1 = HibernateUtil.currentSession();
Transaction txe1 = hibernateSession.beginTransaction();
Query qEnseignant = hibernateSessione1.createQuery("from Enseignant where UserName = :userSID ");
qEnseignant.setParameter("userSID", session.getAttribute("UserName"));
Enseignant en =(Enseignant) qEnseignant.uniqueResult();
Query qLVe = hibernateSessione1.createQuery("from Livre wherea NomLivre = :userSID ");
qLVe.setParameter("userSID", newName);
Livrea LVe =(Livrea) qLVe.uniqueResult();
LVe.addToAjouterenseiSet(en);
int nbre =en.getNbrLivreAjou();
nbre = nbre+1;
en.setNbrLivreAjou(nbre);
hibernateSessione1.update(en);
txe1.commit();
HibernateUtil.closeSession();
}else
if(session.getAttribute("type").equals("Administrateur")){
Session hibernateSessiona1 = HibernateUtil.currentSession();
Transaction txa1 = hibernateSessiona1.beginTransaction();
Query qAdmin = hibernateSessiona1.createQuery("from Administrateur where UserName = :userAdmin ");
qAdmin.setParameter("userAdmin", session.getAttribute("UserName"));
Administrateur admin =(Administrateur) qAdmin.uniqueResult();
Query qLVa = hibernateSessiona1.createQuery("from Livrea where NomLivre = :userLV ");
qLVa.setParameter("userLV", newName);
Livrea LVa =(Livrea) qLVa.uniqueResult();
Ajouter add = new Ajouter();
LVa.addToAjouterSet(admin.getIdAdmin());
if(admin.getNbrLivreAjou()== null)admin.setNbrLivreAjou(1);
else{ int nbra =admin.getNbrLivreAjou();
nbra = nbra+1;
admin.setNbrLivreAjou(nbra);
}
hibernateSessiona1.update(admin);
txa1.commit();
HibernateUtil.closeSession();
}
请帮我
最佳答案
您的代码调用
Livrea LVe =(Livrea)qLVe.uniqueResult();
因此执行的查询应返回一个结果。
但是,执行此查询实际上已返回2条导致异常的结果记录。
根据您的代码应该执行的操作,有两种解决方案:
确保数据库实际上仅返回一个结果记录(例如,通过在用户名列中添加唯一索引)。
如果查询实际上应该返回多个结果记录,请不要使用uniqueResult()方法。