English 中文(简体)
如何在休眠标准中增加区别
原标题:How to add Distinct in Hibernate Criteria

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());

任何想法什么可能是原因, 或其他任何过滤复制物的方法。

问题回答

使用预测。 区别。

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();

您的Bean. class 具有属性“ 类型 ” 。 返回的列表将是 < code> List< yourBean> 。

尝试使用 :

cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

对我来说,这工作很适合我

我终于发现了其他栏目的价值:

Criteria criteria = session.createCriteria(Test.class);
ProjectionList projectionList = Projections.projectionList();
ProjectionList projectionList2 = Projections.projectionList();
projectionList2.add(Projections.distinct(projectionList.add(Projections.property("distinctColumn"), "distinctColumn")));
projectionList2.add(Projections.property("col1"), "col1");
projectionList2.add(Projections.property("col2"), "col2");
criteria.setProjection(projectionList2);
criteria.setResultTransformer(Transformers.aliasToBean(Test.class)); 
List list = criteria.list();

尝试使用 :

Criteria criteria = 
    session.createCriteria(Test.class).setProjection(
        Projections.distinct(Projections.property("testType")));
List<Test> rsList = criteria.list();

遇到同样的问题,最后利用小组预测解决了问题,然后在我需要的所有栏目中加列。

Criteria query = session.createCriteria(Class.class)
    .setProjection(Projections.projectionList()
        .add(Projections.groupProperty("Col1"), "Col1")
        .add(Projections.groupProperty("Col2"), "Col2"))
    .setResultTransformer(Transformers.aliasToBean(Class.class));
List list =  query.list();

尝试 setResult Transfent( 域名. DispINCT_ ROOT_ entity) , 像这样 :

Criteria crit = session.createCriteria(Test.class);
List list = crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

预测只提供标记属性的结果 。 但是, 它会给儿童实体带来问题 。 请看看我所面临真正问题的位置 。

Hibertnate:父母与子女关系数据结构

尝试使用 :

criteria.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

它使用默认的散列码来找到匹配的结果 。

谢谢





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签