title: Getting HibernateException: illegally attempted to associate a proxy with two open Sessions when attempting to update
body: <p>I'm trying to update an element in my table using Hibernate, but I get the following error: </p>
  
  <blockquote>
    <p>There was an unexpected error (type=Internal Server Error,
    status=500). illegally attempted to associate a proxy with two open
    Sessions; nested exception is org.hibernate.HibernateException:
    illegally attempted to associate a proxy with two open Sessions</p>
  </blockquote>
  
  <p>DAO:</p>
  
  <pre><code>    @Override
  public Demande getDemandeById(int id) {
      // TODO Auto-generated method stub
      Session session;
       try {
           session = this.sessionFactory.getCurrentSession();
          } catch (HibernateException ex) {
              session = this.sessionFactory.openSession();
          }
  
      Demande d = (Demande) session.load(Demande.class, new Integer(id));
      return d;
  }
  @Override
  public void updateDemande(Demande d) {
      // TODO Auto-generated method stub
      Session session;
       try {
           session = this.sessionFactory.getCurrentSession();
          } catch (HibernateException ex) {
              session = this.sessionFactory.openSession();
          }
  
      session.update(d);
  
      logger.info("Person updated successfully, Person Details="+d);
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>@GetMapping("rh/editProfilDemande")
  public String  RHEditProfilForm(Model m, @RequestParam int id_demande) {  
      this.profil_id = pf.getIdProfil();
      Demande d = demandeService.getDemandeById(id_demande);
      this.idDemande = id_demande; 
  
      m.addAttribute("profil", pf);
  
      m.addAttribute("demande", d); 
      return "RHEditProfil";
  
  
  }   
  
  @PostMapping("rh/editProfilDemande")
  public String  RHEditProfil(Model m, Profil pf) {
      Demande d= demandeService.getDemandeById(this.idDemande);
  
  
  
      demandeService.updateDemande(d);
       m.addAttribute("demande",d);
      return "RHDemandeTraiteSuccess";
  
  
  } 
  </code></pre>