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

tvmytwxo  于 2022-11-14  发布在  Oracle
关注(0)|答案(1)|浏览(153)

当使用Hibernate通过org.hibernate.Dialt.Oracle10gDialectorg.hibernate.Dialt.OracleDialect从Oracle 11g DB检索数据时,我得到以下信息:

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-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();

如有任何帮助,我们不胜感激。

已解决:

忘了提一下,我使用的是Spring,其中的applationConext.xml如下所示:

<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的属性...
给自己的提示:轻松复制-粘贴

相关问题