You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Oracle11g分页功能失效,请求技术支持

Oracle 11g Spring Boot分页报错ORA-00933解决方案

错误详情

报错信息:

Caused by: org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL [select au1_0.user_id,au1_0.created_on,au1_0.created_by,au1_0.email,au1_0.first_name,au1_0.last_login_date,au1_0.last_name,au1_0.msisdn,au1_0.password,au1_0.security_code,au1_0.status from user au1_0 offset ? rows fetch first ? rows only] [ORA-00933: SQL command not properly ended.

问题原因

Oracle 11g不支持OFFSET ... FETCH FIRST ... ONLY的分页语法,该语法是Oracle 12c才引入的。你的Hibernate默认生成了这种新语法,导致数据库无法识别,抛出SQL语法错误。

解决方案

方法1:配置Oracle 11g兼容的Hibernate方言

在Spring Boot的配置文件中添加以下配置,指定使用Oracle 10g/11g的方言(两者分页逻辑一致):

application.properties

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

application.yml

spring:
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.Oracle10gDialect

配置后,Hibernate会自动生成基于ROWNUM的嵌套查询实现分页,示例SQL如下:

select * from (
    select row_.*, rownum rownum_ from (
        select au1_0.user_id, au1_0.created_on, ... from user au1_0
    ) row_ where rownum <= ?
) where rownum_ > ?

该语法完全兼容Oracle 11g,你的adminUserRepository.findAll(pageable)即可正常工作。

方法2:自定义分页查询(可选)

如果方言配置无法生效,可在Repository层手动编写兼容查询:

public interface AdminUserRepository extends JpaRepository<AdminUser, Long> {
    @Query(value = "SELECT u FROM AdminUser u",
           countQuery = "SELECT COUNT(u) FROM AdminUser u")
    Page<AdminUser> findAll(Pageable pageable);
}

不过只要方言配置正确,默认findAll方法会自动适配,优先推荐方法1。

验证

配置完成后重启项目,调用分页接口,Hibernate生成的SQL会切换为Oracle 11g兼容语法,报错即可解决。

内容的提问来源于stack exchange,提问作者mnjava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 17:18:17