java 休眠,获取重复值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4645434/
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-10-30 07:20:37  来源:igfitidea点击:

Hibernate, getting duplicate values

javahibernatecriteriajbpmhibernate-criteria

提问by Shervin Asgari

I am writing a very simple query, but I am getting duplicate values for some reason.

我正在编写一个非常简单的查询,但由于某种原因我得到了重复的值。

Criteria cr = session.createCriteria(ProcessInstance.class, "p")
        .add(Restrictions.isNull("end"));
@Cleanup ScrollableResults sr = cr.scroll(ScrollMode.FORWARD_ONLY);

while (sr.next()) {
    pi = (ProcessInstance) sr.get(0);
    String id = pi.getId(); //Getting duplicate values
}

The pi.getId()returns duplicate values. ie: *9,9,10,10,11,11 etc*

pi.getId()返回重复值。IE:*9,9,10,10,11,11 etc*

However, running this query directly in mysql

但是,直接在 mysql 中运行此查询

SELECT * FROM JBPM_PROCESSINSTANCE J where J.END_ IS NULL

Does not return duplicate values.

不返回重复值。

Can anyone spot what is wrong?

任何人都可以发现有什么问题吗?

回答by Ralph

The fast workarround would be to use a Distinct Root Entity Result Transformer.

快速解决方法是使用不同的根实体结果转换器。

...
crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List unique = crit.List();
...

But this is only a workarround.

但这只是一种解决方法。

I ques the problem belongs to your mapping. If there is any eager loaded 1:n relationship from ProcessInstance to something else (call it X), and there are several (n) X for one ProcessInstance, the you will get several ProcessInstance items (n) in the result list for a single ProcessInstance. -- If this is realy the cause, than the workarround is not just a workarround, then it would be the solution.

我问这个问题属于你的映射。如果从 ProcessInstance 到其他东西(称为 X)存在任何预先加载的 1:n 关系,并且一个 ProcessInstance 有多个 (n) X,那么您将在结果列表中获得多个 ProcessInstance 项 (n) 以获取单个进程实例。-- 如果这确实是原因,那么 workarround 不仅仅是 workarround,那么它就是解决方案。

回答by seesee

I encouter the same problem as you..

我遇到和你一样的问题..

This is how I solve it.

这就是我解决它的方法。

Criteria cr = session.createCriteria(ProcessInstance.class, "p")
        .add(Restrictions.isNull("end")).setProjection("id")

this will returns all the ID that satisfy all your criteria.

这将返回满足所有条件的所有 ID。

there after you use Inrestrictions and perform CriteriaSpecification.DISTINCT_ROOT_ENTITY.

在您使用In限制并执行CriteriaSpecification.DISTINCT_ROOT_ENTITY.

You will be able to scroll your result in 2nd criteria.. I hope this help.

您将能够在第二个条件中滚动您的结果.. 我希望这会有所帮助。