3

我正在尝试在 JPA 查询中使用 group by。假设我有一个 classTeacher和一个 class Student。ATeacher可以有多个Students,而 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,但这并不方便。有更好的解决方案吗?

4

1 回答 1

5

该查询在 PostgreSQL 版本 9.1 中变得有效。您似乎在本地使用 PostgreSQL 版本 9.1,而 Heroku 使用的是更早的版本。

请参阅 9.1 发行说明:

http://www.postgresql.org/docs/9.1/interactive/release-9-1.html

在该页面的“查询”部分下,它说:

当在 GROUP BY 子句中指定主键时,允许查询目标列表中的非 GROUP BY 列 (Peter Eisentraut)

SQL 标准允许这种行为,并且由于主键,结果是明确的。

要使其在早期版本的 PostgreSQL 下工作,请将选择列表中不使用聚合函数的所有表达式添加到 GROUP BY 子句。

于 2012-04-10T15:26:19.140 回答