【问题标题】:Group by with JPA and PostgreSQL 9.0使用 JPA 和 PostgreSQL 9.0 分组
【发布时间】:2012-04-10 14:59:41
【问题描述】:

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

【问题讨论】:

  • 这是 PostgreSQL 9.0 中最简单的解决方案。您还可以使用 CTE 来处理 GROUP BY 并在 SELECT 中引用 CTE,从而引入其他列,但这并不简单。也许您可以作为 Heroku 提供最新的生产版本,并说明您想要它的原因?
  • CTE 是一种解决方案,但仍不理想。我知道您可以在使用专用数据库时在 Heroku 上升级到 9.1。目前我正在使用共享的,所以我想他们升级到 9.1 可能是个问题......无论如何我都会联系他们!谢谢

标签: hibernate postgresql jpa group-by


【解决方案1】:

该查询在 PostgreSQL 版本 9.1 中生效。看来您在本地使用的是 PostgreSQL 版本 9.1,而 Heroku 使用的是更早的版本。

查看 9.1 版本说明:

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

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

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

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

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

【讨论】:

  • 谢谢,这完美地解释了为什么它不能在 Heroku 上运行。事实上 Heroku 仍在使用 9.0 版本。不过,为什么 JPA 生成组时不指定所有 Teacher 属性而只指定 id?或者如何更改代码以使其在 PostgreSQL 9.0 上运行?谢谢。
  • 我编辑了答案以解释您可以通过将所有非聚合表达式添加到 GROUP BY 子句来解决此问题。
  • 谢谢你,@kgrittn。显然这个解决方案并不理想,但如果它是唯一一个我会将此答案标记为已接受!让我们等待另一个解决方案,如果有的话......谢谢
  • 那个解决方法救了我的命。我们使用的是 PostgreSQL 9.0.3,所以我遇到了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 2011-01-05
  • 2019-10-21
  • 2018-06-02
  • 2017-03-22
  • 1970-01-01
  • 2012-04-04
相关资源
最近更新 更多