简体   繁体   中英

select "all columns" with "group by" in hibernate criteria queries

I want to write a criteria query using "group by" and want to return all the columns.

Plane sql is like this:

select * from Tab group by client_name order by creation_time;

I understand that it will have count(distinct client_name) number of rows.

My current query which doesn't seem to give proper result is as follows:

Criteria criteria = getSession(requestType).createCriteria(Tab.class);
        criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("client_name")));
        criteria.addOrder(Order.asc("creationTime"));

This query returns "client_name" only. I don't want to manually put all column names. There must be some way, what could be done?

I think you're misunderstanding something. If you GROUP BY in SQL, then you need to group by all selected columns. The same applies to Hibernate - if you groupProperty in a Projection , you're telling Hibernate that that column is a group column. If no other columns/fields are referenced, Hibernate will assume you don't want them, as they would also need to be grouped.

To take a step back: what are you trying to do? If you have duplicate data across all columns in a table, you might have bad data, or be persisting data incorrectly. At the very least, your key would be messed up.

In hibernate for projections, all columns required needs to be added to projection list. To get the result in entity we have to use setResultTransformer. Check below example to get group by in hibernate:

 ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("column1"));
    projectionList.add(Projections.property("column2"));
    projectionList.add(Projections.property("column3"));
    criteria.setProjection(projectionList);
    criteria.setResultTransformer(Transformers.aliasToBean(Table.class));

when using hibernate projection you should add all columns which are needed in projection list. you have only used Projections.projectionList().add(Projections.groupProperty("client_name"))

this . so it is clear that return only client_name.

It is not possible to get all columns of the database by using a group based on a single column. Instead, you can use all columns in the group by clause and in the select as well which you want. To get the result in hibernate like below

    Criteria criteria = getSession(requestType).createCriteria(Tab.class);
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("client_name"));
    projectionList.add(Projections.property("column2"));
    projectionList.add(Projections.property("column3"));
    projectionList.add(Projections.groupProperty("client_name"));
    projectionList.add(Projections.groupProperty("column2"));
    projectionList.add(Projections.groupProperty("column3"));
    criteria.setProjection(projectionList);
    criteria.addOrder(Order.asc("creationTime"));

In Hibernate Criteria Queries, if you want to select all columns with "group by" and return the result as a list of entities, you can use the setResultTransformer method to transform the result into a list of entities. Here's an example:

Criteria criteria = getSession(requestType).createCriteria(Tab.class);
criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("client_name")));
criteria.addOrder(Order.asc("creationTime"));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<Tab> results = criteria.list();

The setResultTransformer method is used to transform the results of a criteria query into a specific format. In this case, we are using the DISTINCT_ROOT_ENTITY transformer which will return a list of distinct Tab entities based on the group by "client_name" and order by "creationTime"

Also, you can use Projections.groupProperty("*") instead of Projections.projectionList().add(Projections.groupProperty("client_name")) to group by all columns, but it is not recommended since it is not standard SQL and some DBMS may not support it.

Keep in mind that the performance of this query may be affected by the large number of columns and the number of rows in the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM