简体   繁体   English

Spring 在使用具有 MSSQLSERVER 兼容性的 H2 时,使用数据 R2DBC findById() 引导 2.7.1 失败(语法错误 LIMIT 2)

[英]Spring Boot 2.7.1 with Data R2DBC findById() fails when using H2 with MSSQLSERVER compatibility (bad grammar LIMIT 2)

I'm upgrading a Spring Boot 2.6.9 application to the 2.7.x line (2.7.1).我正在将 Spring Boot 2.6.9 应用程序升级到 2.7.x 行 (2.7.1)。 The application tests use H2 with MS SQL Server compatibility mode.应用测试使用 H2 与 MS SQL 服务器兼容模式。

I've created a simple sample project to reproduce this issue: https://github.com/codependent/boot-h2我创建了一个简单的示例项目来重现此问题: https://github.com/codependent/boot-h2

Branches:分支机构:

  • main: Spring Boot 2.7.1 - Tests KO主要:Spring 引导 2.7.1 - 测试 KO
  • boot26: Spring Boot 2.6.9 - Tests OK boot26:Spring 引导 2.6.9 - 测试正常

To check the behaviour just run ./mvnw clean test要检查行为,只需运行./mvnw clean test

These are the relevant parts of the code:这些是代码的相关部分:

Test application.yml测试应用程序.yml

spring:
  r2dbc:
    url: r2dbc:h2:mem:///testdb?options=MODE=MSSQLServer;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

schema.sql架构.sql

CREATE SCHEMA IF NOT EXISTS [dbo];
CREATE TABLE IF NOT EXISTS [dbo].[CUSTOMER] (
    id INTEGER GENERATED BY DEFAULT AS IDENTITY,
    name VARCHAR(255) NOT NULL,
    CONSTRAINT PK__otp__D444C58FB26C6D28 PRIMARY KEY (id)
);

Entity实体

@Table("[dbo].[CUSTOMER]")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomerEntity {

    @Id
    @Column("id")
    private Integer id;

    @Column("name")
    private String name;

}

Data R2DBC Repository数据 R2DBC 存储库

public interface CustomerRepository extends ReactiveCrudRepository<CustomerEntity, Integer> {
}

The problem occurs when invoking customerRepository.findById(xxx) , as can be seen in the following test调用customerRepository.findById(xxx)时出现问题,如下测试所示

@SpringBootTest
@RequiredArgsConstructor
@TestConstructor(autowireMode = ALL)
class BootH2ApplicationTests {

    private final CustomerRepository customerRepository;

    @Test
    void shouldSaveAndLoadUsers() {

        CustomerEntity joe = customerRepository.save(new CustomerEntity(null, "Joe")).block();

        customerRepository.findById(joe.getId()).block();

Exception:例外:

Caused by: io.r2dbc.spi.R2dbcBadGrammarException: 
Syntax error in SQL statement "SELECT [dbo].[CUSTOMER].* FROM [dbo].[CUSTOMER] WHERE [dbo].[CUSTOMER].id = $1 [*]LIMIT 2"; SQL statement:
SELECT [dbo].[CUSTOMER].* FROM [dbo].[CUSTOMER] WHERE [dbo].[CUSTOMER].id = $1 LIMIT 2 [42000-214]

The R2dbcEntityTemplate is limiting the selectOne query to 2 elements: R2dbcEntityTemplate将 selectOne 查询限制为 2 个元素:

    public <T> Mono<T> selectOne(Query query, Class<T> entityClass) throws DataAccessException {
        return (Mono)this.doSelect(query.getLimit() != -1 ? query : query.limit(2), entityClass, this.getTableName(entityClass), entityClass, RowsFetchSpec::one);
    }

And this is translated into a LIMIT N clause which is not supported by H2/SQL Server.这被翻译成 H2/SQL Server 不支持的 LIMIT N 子句。

Not sure if it's some kind of H2/Spring Data bug or there's a way to fix this.不确定它是某种 H2/Spring Data 错误还是有办法解决这个问题。

It will be solved in Spring Data 2.4.3 (2021.2.3) 在 Spring 数据 2.4.3 (2021.2.3) 中解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM