本文介绍了如何在 Hibernate 标准中添加 Distinct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有一个 Test 表,列有:testName、testType有 2 个不同的测试具有相同的类型,即SUN",所以我只想要其中一个我在我的休眠/标准中使用 Distinct ,如下所示,但它仍然给我两种与sun"同名的类型.

In my database I have a Test table, with columns: testName, testType there are 2 different tests with the same type I.e "SUN", so I want only one of them for which I use Distinct in my hibernate / criteria as below, but it still giving me both the types with the same name as "sun".

        Criteria crit = session.createCriteria(Test.class);

    final ResultTransformer trans = new DistinctRootEntityResultTransformer();
    crit.setResultTransformer(trans);
    List rsList = trans.transformList(crit.list());

知道可能是什么原因,或任何其他过滤重复项的方式.

Any idea what could be the reason, or any other way of filtering duplicates.

推荐答案

Use Projections.distinct.

Use Projections.distinct.

Criteria crit = session.createCriteria(Test.class).setProjection(
    Projections.distinct(Projections.projectionList()
    .add(Projections.property("type"), "type") )
.setResultTransformer(Transformers.aliasToBean(YourBean.class)); 

List lst = crit.list();

其中 YourBean.class 具有属性类型".返回的列表将是 List.

where YourBean.class has a property "type". The returned list will be List<YourBean>.

这篇关于如何在 Hibernate 标准中添加 Distinct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:32