Java Hibernate:错误-列必须出现在GROUPBY子句中,或在聚合函数中使用
我试图将SQL查询更改为CriteriaBuilder方式,并面临此错误Java Hibernate:错误-列必须出现在GROUPBY子句中,或在聚合函数中使用,java,postgresql,hibernate,criteria,hibernate-criteria,Java,Postgresql,Hibernate,Criteria,Hibernate Criteria,我试图将SQL查询更改为CriteriaBuilder方式,并面临此错误 Caused by: org.postgresql.util.PSQLException: ERROR: column "lopcoverag0_.slot" must appear in the GROUP BY clause or be used in an aggregate function SQL-当我在SQLWorkbench中执行此查询时,此查询对我有效 select count(
Caused by: org.postgresql.util.PSQLException:
ERROR: column "lopcoverag0_.slot" must appear in the GROUP BY clause
or be used in an aggregate function
SQL-当我在SQLWorkbench中执行此查询时,此查询对我有效
select count(*) from (Select distinct slot from lop_coverage
where pageFamily='Gateway' and currentStatus ='Active'
and (lop in ("[en_US]","none")) group by creativeID) as totalcount;
到JPA标准生成器:
我知道我在criteriaBuilder中的代码有一个错误,有人能告诉我这个问题或者给我指出正确的地方吗
选择不同的插槽
这不是聚合函数,也不在group by中
或者
选择不同的组、插槽
或选择组、插槽。。。分组,插槽除非creativeID是主键,否则显示的第一个查询也不起作用。由于您现在查询的是具有不同列的不同表,因此它可能不再是主键。是。桌子是不同的。最初,包中的代码是使用JDBC和纯SQL字符串编写的,DB是mysql。现在我正在重构整个代码,以使用hibernate和postgresql。@jjanes我更新了查询。请选中刚刚选中此查询正在使用SQL DB:select count*从lop_coverage中选择不同的插槽,其中page_family='Consumbles'和current_status='Active'和lop位于'en_in','none'组由creative_id作为totalcount。但不是在postgresql中,DBI修改了此查询,从选择不同的创造性\u id中选择计数*,从lop\u覆盖范围中选择插槽,其中执行日期为'2020-07-02',页面为'family='耗材',当前状态为'Active',lop为'en\u in',按创造性\u id选择'none'组,插槽为totalcount不显示错误,但插槽值重复如果您只需要不同的插槽-只需选择不同的插槽而不进行任何分组需要不同的插槽计数进行分组。因为创造性ID是重复的
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<String> criteriaQuery = criteriaBuilder.createQuery(String.class);
Root<LOPCoverage> root = criteriaQuery.from(LOPCoverage.class);
criteriaQuery.select(root.get(LOPCoverage_.slot)).distinct(true);
List<String> lopList = Arrays.asList(new String[]{"none", "[en_US]"});
Expression<String> lopExpression = root.get(LOPCoverage_.lop);
Predicate lopINPredicate = lopExpression.in(lopList);
Predicate andRestriction = criteriaBuilder.and(
criteriaBuilder.equal(root.get("pageFamily"), pageFamily),
lopINPredicate);
criteriaQuery.where(andRestriction).groupBy(root.get("creativeID"));
System.out.println( session.createQuery(criteriaQuery).list());
Hibernate: select distinct lopcoverag0_.slot as col_0_0_ from
lop_coverage lopcoverag0_ where lopcoverag0_.page_family=? and
(lopcoverag0_.lop in (? , ?)) group by lopcoverag0_.creative_id