gpt4 book ai didi

mysql - 在 hibernate 状态下提交事务

转载 作者:行者123 更新时间:2023-11-29 09:07:49 24 4
gpt4 key购买 nike

当我多次使用 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条结果记录,导致了异常。

根据您的代码的用途,有两种解决方案:

  1. 确保数据库实际上只返回一条结果记录(例如,通过向用户名列添加唯一索引。

  2. 如果查询实际上应该返回多个结果记录,请勿使用 uniqueResult() 方法。

关于mysql - 在 hibernate 状态下提交事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6536661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com