Java org.hibernate.NonUniqueResultException:查询没有返回唯一结果:2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23866709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:41:46  来源:igfitidea点击:

org.hibernate.NonUniqueResultException: query did not return a unique result: 2?

javahibernate

提问by user3198603

I have below code in my dao

我的 dao 中有以下代码

String sql = "SELECT COUNT(*) FROM CustomerData WHERE custId = :custId AND deptId = :deptId";

Query query = session.createQuery(sql);
query.setParameter("custId", custId);
query.setParameter("deptId", deptId);
long count =(long) query.uniqueResult(); //line 1

Hibernate throws below exception at line 1

Hibernate 在第 1 行抛出以下异常

    org.hibernate.NonUniqueResultException: query did not return a unique result: 

I am not sure whats happening as count(*) will always return only one row. Also when i run this query on db directly, it return result as 1. So whats the issue?

我不确定发生了什么,因为 count(*) 总是只返回一行。此外,当我直接在 db 上运行此查询时,它返回结果为 1。那么问题是什么?

回答by Maodo

First you must test the query list size; here a example:

首先你必须测试查询列表的大小;这里有一个例子:

long count;
if (query.list().size() > 0)
    count=(long) criteria.list().get(0);   
else
    count=0;            
return count;

回答by GROX13

It seems like your query returns more than one result check the database. In documentation of query.uniqueResult()you can read:

似乎您的查询返回多个结果检查数据库。在query.uniqueResult()您的文档中,您可以阅读:

Throws: org.hibernate.NonUniqueResultException - if there is more than one matching result

抛出: org.hibernate.NonUniqueResultException - 如果有多个匹配结果

If you want to avoid this error and still use unique result request, you can use this kind of workaround query.setMaxResults(1).uniqueResult();

如果您想避免此错误并仍然使用唯一结果请求,则可以使用这种解决方法 query.setMaxResults(1).uniqueResult();

回答by rex roy

It means that the query you wrote returns more than one element(result) while your code expects a single result.

这意味着您编写的查询返回多个元素(结果),而您的代码需要一个结果。

回答by Ian Wang

I don't think other answers explained the key part: why "COUNT(*)" returns more than one result?

我认为其他答案没有解释关键部分:为什么“COUNT(*)”返回多个结果?

I just encountered the same issue today, and what I found out is that if you have another class extending the target mapped class (here "CustomerData"), Hibernate will do this magic.

我今天刚遇到同样的问题,我发现如果你有另一个扩展目标映射类的类(这里是“CustomerData”),Hibernate 会做这个魔术。

Hope this will save some time for other unfortunate guys.

希望这会为其他不幸的人节省一些时间。

回答by Darshan

Basically your query returns more than one result set. In API Docs uniqueResult() method says that Convenience method to return a single instance that matches the query, or null if the query returns no results

基本上,您的查询返回多个结果集。在 API Docs 中的 uniqueResult() 方法表示 Convenience 方法返回与查询匹配的单个实例,如果查询没有返回结果,则返回 null

uniqueResult()method yield only single resultset

uniqueResult()方法只产生单个结果集

回答by u11458207

Hibernate Optional findTopByClientIdAndStatusOrderByCreateTimeDesc(Integer clientId, Integer status);

Hibernate 可选 findTopByClientIdAndStatusOrderByCreateTimeDesc(Integer clientId, Integer status);

"findTop"!! The only one result!

“找顶”!!结果只有一个!

回答by James

Could this exception be thrown during an unfinished transaction, where your application is attempting to create an entity with a duplicate field to the identifier you are using to try find a single entity?

在未完成的事务期间是否会抛出此异常,其中您的应用程序试图创建一个实体,该实体具有与您用来尝试查找单个实体的标识符的重复字段?

In this case the new (duplicate) entity will not be visible in the database as the transaction won't have, and will never be committed to the db. The exception will still be thrown however.

在这种情况下,新的(重复的)实体在数据库中将不可见,因为事务没有,并且永远不会提交到数据库。但是,仍然会抛出异常。

回答by Pathum Lakshan

Thought this might help to someone, it happens because "When the number of data queries is greater than 1".reference

认为这可能对某人有所帮助,但发生这种情况是因为“当数据查询的数量大于 1 时”。参考

回答by Majid Roustaei

Generally This exception is thrown from Oracle when query result (which is stored in an Object in your case), can not be cast to the desired object. for example when result is a

通常,当查询结果(在您的情况下存储在对象中)无法转换为所需的对象时,Oracle 会抛出此异常。例如,当结果是

List<T>

and you're putting the result into a single T object.

并且您将结果放入单个 T 对象中。

In case of casting to long error, besides it is recommended to use wrapper classes so that all of your columns act the same, I guess a problem in transaction or query itself would cause this issue.

在强制转换为长错误的情况下,除了建议使用包装器类以便所有列的行为都相同之外,我猜事务或查询本身的问题会导致此问题。