【问题标题】:spring JPA Hibernate MariaDB春季 JPA Hibernate MariaDB
【发布时间】:2018-06-27 18:16:31
【问题描述】:

我正在尝试从我的 spring JPA hibernate 应用程序连接到 mariaDB v10.2.14。我正在使用

driverClassName: com.mysql.jdbc.Driver

setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

我有一个查询方法来查找第一行,通过使用“findTop1”关键字...当查询实际形成时,它使用 'offset 0 rows fetch next 1 rows only' 而不是 LIMIT 0,1 。 .. 正因为如此,maria DB 服务器会抛出一个错误...

如何更改我的驱动程序/方言文件以使用 LIMIT 子句而不是 offset-fetch 来形成查询..

注意:我已经尝试了 mariadbdialect 和 mariadb 驱动程序类名称的所有组合,例如 org.hibernate.dialect.MariaDB53Dialect 和 org.mariadb.jdbc.Driver..

以下是我遇到的异常

2018-06-27 11:31:19.936  INFO 10484 --- [nio-8080-exec-2] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select aboutpage0_.id as id1_0_, aboutpage0_.APPLICATION_NAME as APPLICAT2_0_, aboutpage0_.createdDate as createdD3_0_, aboutpage0_.effectiveDate as effectiv4_0_, aboutpage0_.LAST_UPD_TS as LAST_UPD5_0_, aboutpage0_.LAST_UPD_SYSUSR_ID as LAST_UPD6_0_, aboutpage0_.version as version7_0_ from XXXXX aboutpage0_ where aboutpage0_.APPLICATION_NAME=? and aboutpage0_.effectiveDate<=? order by aboutpage0_.LAST_UPD_TS desc, aboutpage0_.effectiveDate desc offset 0 rows fetch next ? rows only
2018-06-27 11:31:20.112  WARN 10484 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1064, SQLState: 42000
2018-06-27 11:31:20.112 ERROR 10484 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'offset 0 rows fetch next 1 rows only' at line 1
2018-06-27 11:31:20.142 ERROR 10484 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'offset 0 rows fetch next 1 rows only' at line 1

【问题讨论】:

标签: mysql spring hibernate spring-data-jpa mariadb


【解决方案1】:

漂亮地打印查询:

select  aboutpage0_.id as id1_0_, aboutpage0_.APPLICATION_NAME as APPLICAT2_0_,
        aboutpage0_.createdDate as createdD3_0_, aboutpage0_.effectiveDate as effectiv4_0_,
        aboutpage0_.LAST_UPD_TS as LAST_UPD5_0_, aboutpage0_.LAST_UPD_SYSUSR_ID as LAST_UPD6_0_,
        aboutpage0_.version as version7_0_
    from  XXXXX aboutpage0_
    where  aboutpage0_.APPLICATION_NAME=?
      and  aboutpage0_.effectiveDate<=?
    order by  aboutpage0_.LAST_UPD_TS desc,
              aboutpage0_.effectiveDate desc
    offset 0 rows fetch next ? rows only 2018-06-27 

应该清楚地表明从 Hibernate 创建 SQL 时出现了问题。它可能会说limit 1 offset 0,但失败了。 (注意:LIMIT 位于 OFFSET 之前。)

【讨论】:

  • 你是说我使用了错误的驱动程序/方言,还是说 ASTQueryTranslatorFactory 有问题?
猜你喜欢
  • 1970-01-01
  • 2020-09-02
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 2013-11-12
  • 1970-01-01
  • 2023-01-25
相关资源
最近更新 更多