简体   繁体   中英

Spring Boot Non Unique Result Exception

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";
 }

您的 DAO 方法返回不止一行,但您有一个值作为定义的结果类型而不是集合。

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>
   

As Simon Martinelli mentioned DAO method "findBy..." returns more than one row, if you need only one row you should use "findFirst...".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM