HIbernate:发现具有给定标识符的多于一行 [英] HIbernate : More than one row with the given identifier was found

查看:187
本文介绍了HIbernate:发现具有给定标识符的多于一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
    public Application getApplicationForId(Long applicationId) {

        List<Application> applications = executeNamedQuery("applicationById", Application.class, applicationId);
        return applications.isEmpty() ? null : applications.get(0);
    }

在Eclipse中调试时

while debugging in eclipse

返回application.isEmpty()吗? null:applications.get(0);

return applications.isEmpty() ? null : applications.get(0);

这些表达式被评估为

applications.isEmpty()->假

applications.get(0)->(id = 171)

applications.size()-> 1

但在执行此行后会引发抛出错误

but after the execution of this line its throwing error

org.hibernate.HibernateException:找到多个具有给定标识符的行:263536,

即使其大小显示为1,也仍然说明为什么以及如何在执行后获取多行.

Even its size is showing as 1, then still why and how its getting multiple rows after the execution.

推荐答案

我很确定这是由于渴望获取.因此,请检查您的实体并删除fetch=FetchType.EAGER.

I'm quite sure that this is due to eager fetching. So check you entity and remove the fetch=FetchType.EAGER.

实际上,这不是不是,是由于数据库中的重复行引起的,因为显然不可能有重复的主键.相反,这是由Hibernate查找对象并急于填写关系引起的. Hibernate假定将返回一行,但由于有两个与该关系相关联的对象,因此又返回了两行.

Actually this is not caused by duplicate rows in the database, as it's obviously not possible to have duplicate primary keys. Instead this was caused by Hibernate looking up an object, and eagerly filling in a relationship. Hibernate assumed a single row would come back, but two came back because there were two objects associated with that relationship.

这篇关于HIbernate:发现具有给定标识符的多于一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆