Spring Boot Non Unique Result Exception

Viewed 10832

Hello everyone I am using SpringBoot with Mysql . I have this error when i try to show information

I have below code in my controller :

query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2
org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2

Code

 @GetMapping("showdeveforma/{id}")
 public String ShowDeveFormation(Model m , @PathVariable Long id)

 { 
     Formation frm = frmreop.findById(id).get();

     m.addAttribute("avis", srv.findByforma(frm));

     return"ChefProjetAffichageAffectationDeveForma";
 }
3 Answers

Your DAO method returns more than one row but you have a single value as the result type defined and not a Collection.

Your repository's return type is a single value: srv.findByforma(frm). The query returns more then one results. You can use a collection if it is the expected behaviour or you need to investigate why more than one entity exists to the given Formation. (Check your entity relationships)

I retry this but i can't resolve information Controller :

 @GetMapping("showdeveforma/{id}")
 public String ShowDeveFormation(Model m , @PathVariable Long id)

 { 
    //Formation frm = frmreop.findById(id).get();
     
    // m.addAttribute("avis", srv.findByforma(frm));
    m.addAttribute("ide", id);
    m.addAttribute("avis" , srv.getAvisFormation());
    return"ChefProjetAffichageAffectationDeveForma";
 }

and my page html is :

<thead>
<tr>  

<td> Titre Formation </td>

<td> Description Formation </td>
<td> Adresse Formation </td>
<td> Formateur </td>
<td>Telephone Foramteur  </td>
<td> Nom Devellopeur </td>
<td> Prenom Devellopeur  </td>
<td> Telephone Devellopeur </td>
<td> Emain Devellopeur</td>
</tr>
</thead>
<tbody>
<tr th:each="avis : ${avis}"  th:if="${avis.forma.id} == 'ide'">
<td th:text="${avis.forma.NomFormation}"> </td>
<td th:text="${avis.forma.DescriptionFormation}"> </td>
<td th:text="${avis.forma.AdresseFormation}"> </td>
<td th:text="${avis.forma.formateurs.PrenomFormateur}"> </td>
<td th:text="${avis.forma.formateurs.TelephoneFormateur}"> </td>
<td th:text="${avis.deve.NomDeve}"> </td>
<td th:text="${avis.deve.PrenomDeve}"> </td>
<td th:text="${avis.deve.TelephoneDeve}"> </td>
<td th:text="${avis.deve.EmailDeve}"> </td>
</tr>   
</tbody>



</table>
   
Related