Oracle 11g上的Hibernate Criteria.setMaxResults()失败

时间:2012-06-19 13:05:40

标签: java oracle hibernate pagination paging

使用hibernate从 org.hibernate.dialect.Oracle10gDialect org.hibernate.dialect.OracleDialect 检索Oracle 11g数据库中的数据时 我得到以下内容:

org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected

查看日志,我们可以看到查询:

select top ? this_.LI_ILN as LI1_8_0_, this_.COUNTRY_CODE ... 

显然,DB无法识别关键字,这就是问题所在,因为在Oracle中,分页只能通过使用 ROWNUM 来完成,这是Hibernate应该知道的事情。

hibernate配置如下所示:

<hibernate-configuration>
<session-factory name="HibernateSessionFactory">
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.password">...</property>
    <property name="hibernate.connection.url">...</property>
    <property name="hibernate.connection.username">...</property>
    <property name="hibernate.default_schema">...</property>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">validate</property>
    <property name="hibernate.transaction.auto_close_session">false</property>

    <mapping resource="HB_Mappings/Supplier.hbm.xml" />
</session-factory>

查询完成如下:

Criteria crit = sessionFactory.getCurrentSession().createCriteria(Supplier.class);
crit.setFirstResult(50 * pageIndex);
crit.setMaxResults(50);

List<Supplier> list = crit.list();

感谢任何帮助。


解决:

忘记提到我使用的是applicationContext.xml的spring:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:HB_Mappings/hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
    </property>
</bean>

它覆盖了hibernate.cfg.xml的属性......

自我注意:轻松复制粘贴

1 个答案:

答案 0 :(得分:1)

OracleDialect已弃用,请参见此处: http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/dialect/OracleDialect.html

改为使用Oracle10gDialect:http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/dialect/Oracle10gDialect.html

另外检查一下,确保您使用的是最新的Oracle JDBC驱动程序。

https://community.jboss.org/wiki/SupportedDatabases2