与JPA和PostgreSQL 9.0分组

时间:2012-04-10 14:59:41

标签: hibernate postgresql jpa group-by

我正在尝试在JPA查询中使用该组。假设我有一个班级Teacher和一个班级StudentTeacher可以包含更多Student个而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以错误的方式生成组:它应该包括所有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,但这不是很方便。有更好的解决方案吗?

1 个答案:

答案 0 :(得分:5)

该查询在PostgreSQL 9.1版中生效。看来你在本地使用PostgreSQL版本9.1,而Heroku正在使用更早的东西。

请参阅9.1发行说明:

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

在该页面的Queries部分下,它显示:

  

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

     

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

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