与JPA和PostgreSQL 9.0分组

sat*_*shi 3 postgresql hibernate jpa group-by

我正在尝试在JPA查询中使用该组.假设我有班级Teacher和班级Student.A Teacher可以有更多的Students,而a 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");
Run Code Online (Sandbox Code Playgroud)

生成以下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
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

谢谢!

更新 - 解决方案是指定GROUP BY teacher.id, teacher.name而不是GROUP BY teacher,但这不是很方便.有更好的解决方案吗?

kgr*_*ttn 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子句中.