0

i get this error when i'm using tx.commit more then one time

Report type'exception

 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

this is my code:

   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();  
  }

plz help me

1

1 Answer 1

2

Your code calls

Livrea LVe =(Livrea) qLVe.uniqueResult();

so the query that is executed should return one single result. However executing this query actually has returned 2 result records which caused the exception.

There are two solutions based on what your code is supposed to do:

  1. Make sure that the database actually returns only one result record (e.g. by adding a unique index to the username column.

  2. Do not use the uniqueResult() method if the query is actually supposed to return more than one result record.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.