Skip to content

Commit 46826bc

Browse files
committed
Fix derived queries with boolean literals.
`IsTrue` and `IsFalse` queries no longer use a literal in the query, but a bind parameter. This allows Spring Data JDBC or the JDBC driver to convert the passed boolean value to whatever is required in the database. For Oracle converter where added to support storing and loading booleans as NUMBER(1,0) where 0 is false and everything else is true. Closes #908
1 parent 2a61a54 commit 46826bc

File tree

12 files changed

+76
-16
lines changed

12 files changed

+76
-16
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/QueryMapper.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,15 @@ private Condition createCondition(Column column, @Nullable Object mappedValue, i
402402
}
403403

404404
if (comparator == Comparator.IS_TRUE) {
405-
return column.isEqualTo(SQL.literalOf(true));
405+
406+
Expression bind = bindBoolean(column, parameterSource, true);
407+
return column.isEqualTo(bind);
406408
}
407409

408410
if (comparator == Comparator.IS_FALSE) {
409-
return column.isEqualTo(SQL.literalOf(false));
411+
412+
Expression bind = bindBoolean(column, parameterSource, false);
413+
return column.isEqualTo(bind);
410414
}
411415

412416
Expression columnExpression = column;
@@ -495,6 +499,12 @@ private Condition createCondition(Column column, @Nullable Object mappedValue, i
495499
}
496500
}
497501

502+
private Expression bindBoolean(Column column, MapSqlParameterSource parameterSource, boolean value) {
503+
504+
Object converted = converter.writeValue(value, ClassTypeInformation.OBJECT);
505+
return bind(converted, Types.BIT, parameterSource, column.getName().getReference());
506+
}
507+
498508
Field createPropertyField(@Nullable RelationalPersistentEntity<?> entity, SqlIdentifier key) {
499509
return entity == null ? new Field(key) : new MetadataBackedField(key, entity, mappingContext, converter);
500510
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,19 @@ public void pageQueryProjectionShouldReturnProjectedEntities() {
492492
assertThat(result.getContent().get(0).getName()).isEqualTo("Entity Name");
493493
}
494494

495+
@Test // #908
496+
void derivedQueryWithBooleanLiteralFindsCorrectValues() {
497+
498+
repository.save(createDummyEntity());
499+
DummyEntity entity = createDummyEntity();
500+
entity.flag = true;
501+
entity = repository.save(entity);
502+
503+
List<DummyEntity> result = repository.findByFlagTrue();
504+
505+
assertThat(result).extracting(e -> e.idProp).containsExactly(entity.idProp);
506+
}
507+
495508
private Instant createDummyBeforeAndAfterNow() {
496509

497510
Instant now = Instant.now();
@@ -557,6 +570,8 @@ interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
557570

558571
@Query("SELECT * FROM DUMMY_ENTITY WHERE OFFSET_DATE_TIME > :threshhold")
559572
List<DummyEntity> findByOffsetDateTime(@Param("threshhold") OffsetDateTime threshhold);
573+
574+
List<DummyEntity> findByFlagTrue();
560575
}
561576

562577
@Configuration
@@ -603,10 +618,12 @@ public void onApplicationEvent(AbstractRelationalEvent<?> event) {
603618
@Data
604619
@NoArgsConstructor
605620
static class DummyEntity {
621+
606622
String name;
607623
Instant pointInTime;
608624
OffsetDateTime offsetDateTime;
609625
@Id private Long idProp;
626+
boolean flag;
610627

611628
public DummyEntity(String name) {
612629
this.name = name;

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQueryUnitTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828
import java.util.Properties;
2929

30+
import org.junit.jupiter.api.Disabled;
3031
import org.junit.jupiter.api.Test;
3132
import org.junit.jupiter.api.extension.ExtendWith;
3233
import org.mockito.junit.jupiter.MockitoExtension;
@@ -457,7 +458,7 @@ public void createsQueryToFindAllEntitiesByBooleanAttributeTrue() throws Excepti
457458
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
458459
ParametrizedQuery query = jdbcQuery.createQuery(accessor, returnedType);
459460

460-
assertThat(query.getQuery()).isEqualTo(BASE_SELECT + " WHERE " + TABLE + ".\"ACTIVE\" = TRUE");
461+
assertThat(query.getQuery()).isEqualTo(BASE_SELECT + " WHERE " + TABLE + ".\"ACTIVE\" = :active");
461462
}
462463

463464
@Test // DATAJDBC-318
@@ -468,7 +469,7 @@ public void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Except
468469
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
469470
ParametrizedQuery query = jdbcQuery.createQuery(accessor, returnedType);
470471

471-
assertThat(query.getQuery()).isEqualTo(BASE_SELECT + " WHERE " + TABLE + ".\"ACTIVE\" = FALSE");
472+
assertThat(query.getQuery()).isEqualTo(BASE_SELECT + " WHERE " + TABLE + ".\"ACTIVE\" = :active");
472473
}
473474

474475
@Test // DATAJDBC-318

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryIntegrationTests-db2.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ CREATE TABLE dummy_entity
55
id_Prop BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY,
66
NAME VARCHAR(100),
77
POINT_IN_TIME TIMESTAMP,
8-
OFFSET_DATE_TIME TIMESTAMP -- with time zone is only supported with z/OS
8+
OFFSET_DATE_TIME TIMESTAMP, -- with time zone is only supported with z/OS
9+
FLAG BOOLEAN
910
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryIntegrationTests-h2.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ CREATE TABLE dummy_entity
33
id_Prop BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY,
44
NAME VARCHAR(100),
55
POINT_IN_TIME TIMESTAMP,
6-
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE
6+
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
7+
FLAG BOOLEAN
78
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryIntegrationTests-hsql.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ CREATE TABLE dummy_entity
33
id_Prop BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY,
44
NAME VARCHAR(100),
55
POINT_IN_TIME TIMESTAMP,
6-
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE
6+
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
7+
FLAG BOOLEAN
78
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryIntegrationTests-mariadb.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ CREATE TABLE dummy_entity
33
id_Prop BIGINT AUTO_INCREMENT PRIMARY KEY,
44
NAME VARCHAR(100),
55
POINT_IN_TIME TIMESTAMP(3),
6-
OFFSET_DATE_TIME TIMESTAMP(3)
6+
OFFSET_DATE_TIME TIMESTAMP(3),
7+
FLAG BOOLEAN
78
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryIntegrationTests-mssql.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ CREATE TABLE dummy_entity
44
id_Prop BIGINT IDENTITY PRIMARY KEY,
55
NAME VARCHAR(100),
66
POINT_IN_TIME DATETIME,
7-
OFFSET_DATE_TIME DATETIMEOFFSET
7+
OFFSET_DATE_TIME DATETIMEOFFSET,
8+
FLAG BIT
89
);
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
SET SQL_MODE='ALLOW_INVALID_DATES';
22

3-
CREATE TABLE dummy_entity
3+
CREATE TABLE DUMMY_ENTITY
44
(
5-
id_Prop BIGINT AUTO_INCREMENT PRIMARY KEY,
5+
ID_PROP BIGINT AUTO_INCREMENT PRIMARY KEY,
66
NAME VARCHAR(100),
7-
POINT_IN_TIME TIMESTAMP(3) default null,
8-
OFFSET_DATE_TIME TIMESTAMP(3) default null
7+
POINT_IN_TIME TIMESTAMP(3) DEFAULT NULL,
8+
OFFSET_DATE_TIME TIMESTAMP(3) DEFAULT NULL,
9+
FLAG BIT(1)
910
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryIntegrationTests-oracle.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ CREATE TABLE DUMMY_ENTITY
55
ID_PROP NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
66
NAME VARCHAR2(100),
77
POINT_IN_TIME TIMESTAMP,
8-
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE
8+
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
9+
FLAG NUMBER(1,0)
910
);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryIntegrationTests-postgres.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ CREATE TABLE dummy_entity
44
id_Prop SERIAL PRIMARY KEY,
55
NAME VARCHAR(100),
66
POINT_IN_TIME TIMESTAMP,
7-
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE
7+
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
8+
FLAG BOOLEAN
89
);

spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OracleDialect.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
*/
1616
package org.springframework.data.relational.core.dialect;
1717

18+
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.data.convert.ReadingConverter;
20+
import org.springframework.data.convert.WritingConverter;
21+
1822
import java.util.Collection;
1923
import java.util.Collections;
2024

25+
import static java.util.Arrays.*;
26+
2127
/**
2228
* An SQL dialect for Oracle.
2329
*
@@ -47,7 +53,25 @@ public IdGeneration getIdGeneration() {
4753

4854
@Override
4955
public Collection<Object> getConverters() {
50-
return Collections.singletonList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE);
56+
return asList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE, NumberToBooleanConverter.INSTANCE, BooleanToIntegerConverter.INSTANCE);
57+
}
58+
59+
@ReadingConverter
60+
enum NumberToBooleanConverter implements Converter<Number, Boolean> {
61+
INSTANCE;
62+
63+
@Override
64+
public Boolean convert(Number number) {
65+
return number.intValue() != 0;
66+
}
5167
}
68+
@WritingConverter
69+
enum BooleanToIntegerConverter implements Converter<Boolean, Integer> {
70+
INSTANCE;
5271

72+
@Override
73+
public Integer convert(Boolean bool) {
74+
return bool ? 1 : 0;
75+
}
76+
}
5377
}

0 commit comments

Comments
 (0)