`

处理JPA异常: More than one row with the given id entifier was found

阅读更多

在使用hibernate entitymanager的时候,我们会用 entityManager.createQuery("xxx").getResultList(); 来获取对象。在某些时候,就会产生如下的异常:

 

 More than one row with the given identifier was found

 这是由getResultList()抛出来的。由于所搜索出的数据会包含重复的id,同时在对象映射上使用@ManyToOne或者@OneToOne,因此hibernate产生异常。

 

由于hibernate会根据搜索语句和关联的对象自动产生很多sql语句,因此要调试的话首先把hibernate sql log打开,把报错的最后一个sql取出来单独执行,看看是否产生重复数据。

 

基本的解决方法就是把重复的数据处理掉,就不会产生异常了。

分享到:
评论
1 楼 失语的乌鸦 2011-10-12  

相关推荐

Global site tag (gtag.js) - Google Analytics