I'm upgrading a Spring Boot 2.6.9 application to the 2.7.x line (2.7.1). The application tests use H2 with MS SQL Server compatibility mode.
I've created a simple sample project to reproduce this issue: https://github.com/codependent/boot-h2
Branches:
To check the behaviour just run ./mvnw clean test
These are the relevant parts of the code:
Test application.yml
spring:
r2dbc:
url: r2dbc:h2:mem:///testdb?options=MODE=MSSQLServer;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
schema.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
public interface CustomerRepository extends ReactiveCrudRepository<CustomerEntity, Integer> {
}
The problem occurs when invoking customerRepository.findById(xxx)
, as can be seen in the following test
@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:
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.
Not sure if it's some kind of H2/Spring Data bug or there's a way to fix this.
It will be solved in Spring Data 2.4.3 (2021.2.3)