我正在尝试在 JPA 查询中使用 group by。假设我有一个 classTeacher
和一个 class Student
。ATeacher
可以有多个Student
s,而 aStudent
可以只有一个Teacher
(一对多)。
以下 JPA 查询:
Query q = this.em.createQuery( "SELECT teacher, COUNT(student)" +
" FROM StudentJpa student" +
" JOIN student.teacher teacher" +
" GROUP BY teacher" +
" ORDER BY COUNT(student) DESC");
生成以下 SQL 查询:
select
teacherjpa1_.teacher_id as col_0_0_,
count(studentjpa0_.id) as col_1_0_,
teacherjpa1_.teacher_id as teacher1_0_,
teacherjpa1_.name as name0_
from
student studentjpa0_
inner join
teacher teacherjpa1_
on studentjpa0_.teacher_id=teacherjpa1_.teacher_id
group by
teacherjpa1_.teacher_id
order by
count(studentjpa0_.id) DESC
在 PostgreSQL 9.0 上,我收到以下错误:
org.postgresql.util.PSQLException:错误:列“teacherjpa1_.name”必须出现在 GROUP BY 子句中或在聚合函数中使用
PostgreSQL 9.1 中没有出现相同的错误。
谁能解释我为什么?似乎 JPA 以错误的方式生成 group by:它应该包括所有Teacher
属性,而不仅仅是 id。
这是我的 JPA/Hibernate/DB 配置,如有必要:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="/WEB-INF/jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
</constructor-arg>
</bean>
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="showSql" value="${db.showSql}" />
<property name="generateDdl" value="${db.generateDdl}" />
</bean>
<!-- enabling annotation driven configuration /-->
<context:annotation-config />
<context:component-scan base-package="my.package" />
<!-- Instructs the container to look for beans with @Transactional and decorate them -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- FactoryBean that creates the EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- A transaction manager for working with JPA EntityManagerFactories -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
谢谢!
更新- 一种解决方案是指定GROUP BY teacher.id, teacher.name
而不是GROUP BY teacher
,但这并不方便。有更好的解决方案吗?