【问题标题】:hibernate- querying for rows, with distinct pair of values from two columnshibernate-查询行,具有来自两列的不同值对
【发布时间】:2013-03-07 11:42:48
【问题描述】:

我有一张这样的桌子:

id|column_1|column_2|column_3|
 0|   A    |   100  |   10   |
 1|   B    |   100  |   20   |
 2|   C    |   1000 |   10   |
 3|   D    |   100  |   10   |

并且我想查询这样我想要不同的(column_2 和 column_3),这意味着 column_2 和 column_3 的组合必须是不同的。所以我想要的结果是:

id|column_1|column_2|column_3|
 0|   A    |   100  |   10   |
 1|   B    |   100  |   20   |
 2|   C    |   1000 |   10   |

我正在使用 STS MVC 和 hibernate4,以及 mysql。任何建议表示赞赏。

我现在使用的代码是:

Session ses=sf.getCurrentSession();
Criteria criteria=ses.createCriteria(myclass.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("column_2"));
projList.add(Projections.property("column_3"));
criteria.setProjection(Projections.distinct(projList));

但它只是返回不同的值,我想要完整的行。

【问题讨论】:

    标签: java spring-mvc hibernate-4.x


    【解决方案1】:

    试试这个:

    select column1, column2, column3 from table group by column2, column3
    

    问候, 阿伦

    【讨论】:

    • 感谢@Abertil 的帮助,但很抱歉这不是答案。该查询只返回分组数据(基于指定的列)。要求是根据不同的值对获取行。另外,我不是在寻找查询,而是在寻找休眠代码。
    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多