Hibernate - groupby和sortby

时间:2016-06-17 10:38:07

标签: java group-by hibernate-criteria

这是我的代码:

    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ApplicantTO.class, "applicant");
    detachedCriteria.setProjection(Projections.distinct(Projections.property("applicant.contact.contactId")));
    detachedCriteria.add(Restrictions.eq("applicant.backToBackMeeting.backToBackMeetingId", backToBackMeetingId));

    // create criteria
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ContactTO.class, "contact");
    criteria.createAlias("contact.representedCountry", "representedCountry", JoinType.LEFT_OUTER_JOIN);
    criteria.add(Restrictions.eq("contact.merged", false));
    criteria.add(Property.forName("contact.contactId").in(detachedCriteria));

    // set pagination and order
    criteria.setMaxResults(pageSize).setFirstResult(first);
    for (String sortField : sortProperties) {
        criteria.addOrder(Boolean.TRUE.equals(order) ? Order.asc(sortField) : Order.desc(sortField));
    }

    return criteria.list();

如您所见,ApplicantTO和ContactTO是两个不同的实体:每个申请人都连接到联系人,但不是相反。

sortProperties属于Contact,但我想添加一个与申请人相关的新sortProperty。

我的想法是拥有这样的东西:

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ApplicantTO.class, "applicant");
criteria.createAlias("applicant.contact", "contact", JoinType.LEFT_OUTER_JOIN);
criteria.createAlias("contact.representedCountry", "representedCountry", JoinType.LEFT_OUTER_JOIN);
criteria.add(Restrictions.eq("applicant.backToBackMeeting.backToBackMeetingId", backToBackMeetingId));
criteria.add(Restrictions.eq("contact.merged", false));
for (ApplicantTO applicant : (List<ApplicantTO>) criteria.list()) {
    Hibernate.initialize(applicant.getContact());
}

// set pagination and order
criteria.setMaxResults(pageSize).setFirstResult(first);
for (String sortField : sortProperties) {
    criteria.addOrder(Boolean.TRUE.equals(order) ? Order.asc(sortField) : Order.desc(sortField));
}
criteria.setProjection(Projections.groupProperty("contact"));

return criteria.list();

但我收到以下例外:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Column "dbo.C_CONTACT.LASTNAME" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:281)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
    ... 154 more

有没有办法对实体的属性进行排序,然后再由另一个实体进行分组?

由于

0 个答案:

没有答案