select “all columns” with “group by” in hibernate criteria queries

不羁岁月 提交于 2019-12-05 01:23:15

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!