Hibernate标准:Distinct和orderby

时间:2017-07-23 21:59:54

标签: oracle-sqldeveloper hibernate-criteria

如何使用distinct和order by在条件查询中从oracle中选择列。我能够使用不同的或有效的顺序,但不能同时使用它们。 我在标准中遇到的问题,对于一列是不同的,我必须根据同一个表中的另一列来排序结果。

Criteria cr = sessionFactory.getCurrentSession().createCriteria(example.class);
cr.add(Restrictions.eq("quote", quote));
cr.add(Restrictions.eq("optn", optn));
cr.add(Restrictions.eq("prdct",prod));
//Adding distinct to show only distinct messages,
ProjectionList projection = Projections.projectionList();
projection.add(Projections.distinct(Projections.property(("Message"))),"Message");
projection.add(Projections.property("screenNo"),"screenNo");
cr.setProjection(projection);
cr.addOrder(Order.asc("screenNo"));

cr.setResultTransformer(new AliasToBeanResultTransformer(example.class));
resultList=cr.list();

示例表

quote |S option |S  message  |S               screenNo
1123  |S 11 |S    This screen is about policy |S      100
2222  |S 22 |S    This screen is about loss |S      103
3333  |S 33   |S  This screen is about term |S       102
4444  |S 44 |S    This screen is about policy    |S   101       

0 个答案:

没有答案