【发布时间】:2012-06-24 14:28:19
【问题描述】:
当使用休眠从 Oracle 11g 数据库中检索数据时,使用 org.hibernate.dialect.Oracle10gDialect 或 org.hibernate.dialect.OracleDialect 我得到以下信息:
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 ...
很明显,数据库无法识别关键字,这就是问题所在,因为在 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,其中 applicationContext.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 的属性...
自我提醒:复制粘贴要轻松
【问题讨论】:
标签: java oracle hibernate pagination paging