MySQL syntax error in generated JPA Repository Pageable query: near 'offset x rows fetch first y rows only'

huangapple go评论79阅读模式
英文:

MySQL syntax error in generated JPA Repository Pageable query: near 'offset x rows fetch first y rows only'

问题

I am trying to get some rows from my table with pagination by Spring Boot JPA Repository, but MySQL keeps showing a syntax error in the query that was generated by JPA.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'offset 0 rows fetch first 1 rows only' at line 1

This is my repository

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Integer> {
    ArrayList<MyEntity> findById(Integer id, Pageable pageable);
}

and the generated query by JPA

select ... from myTable c1_0 order by c1_0.id desc offset 0 rows fetch first 1 rows only

Perhaps, I am still using JDBC driver for SQL
Here is a part in pom.xml file

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.33</version>
</dependency>

And here is application.properties

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.database=mysql

How can I get rid of this?

英文:

I am trying to get some rows from my table with pagination by Spring Boot JPA Repository, but MySQL keeps showing a syntax error in the query that was generated by JPA.
It seems MySQL only supports limit y offset x, instead of offset x rows fetch first y rows only (I am testing with the first page, page: 0 and page_size: 1

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#39;offset 0 rows fetch first 1 rows only&#39; at line 1

This is my repository

@Repository
public interface MyRepository extends JpaRepository&lt;MyEntity, Integer&gt; {
    ArrayList&lt;MyEntity&gt; findById(Integer id, Pageable pageable);
}

and the generated query by JPA

select ... from myTable c1_0 order by c1_0.id desc offset 0 rows fetch first 1 rows only

Perhaps, I am still using JDBC driver for SQL
Here is a part in pom.xml file

&lt;dependency&gt;
    &lt;groupId&gt;com.mysql&lt;/groupId&gt;
    &lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;
    &lt;version&gt;8.0.33&lt;/version&gt;
&lt;/dependency&gt;

And here is application.properties

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.database=mysql

How can I get rid of this?

答案1

得分: 0

问题是我的项目正在使用来自Spring Cloud配置服务器的配置。

spring.jpa.properties.hibernate.dialect 配置值为 org.hibernate.dialect.H2Dialect。这就是为什么JPA会生成与MySQL不完全兼容的SQL查询(在我的情况下是 offsetlimit)。

通过使用VM选项将其改回 org.hibernate.dialect.MySQLDialect 解决了我的问题。

英文:

The problem is my project is using the configuration from a Spring cloud config server.

The spring.jpa.properties.hibernate.dialect config value was org.hibernate.dialect.H2Dialect. That's why JPA keep generating SQL query that didn't fully work with MySQL (in my case: offset and limit).

Changing it back to org.hibernate.dialect.MySQLDialect using VM options solved my problem.

huangapple
  • 本文由 发表于 2023年6月29日 09:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定