Skip to content

Commit 5266155

Browse files
committed
Do not convert binary data in byte[].
Closes #1900
1 parent 9be01a3 commit 5266155

11 files changed

+78
-24
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery {
9292
public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
9393
@Nullable RowMapper<?> defaultRowMapper, JdbcConverter converter,
9494
QueryMethodEvaluationContextProvider evaluationContextProvider) {
95-
this(queryMethod.getRequiredQuery(), queryMethod, operations, result -> (RowMapper<Object>) defaultRowMapper, converter, evaluationContextProvider);
95+
this(queryMethod.getRequiredQuery(), queryMethod, operations, result -> (RowMapper<Object>) defaultRowMapper,
96+
converter, evaluationContextProvider);
9697
}
9798

9899
/**
@@ -244,7 +245,7 @@ private JdbcValue writeValue(@Nullable Object value, TypeInformation<?> typeInfo
244245
TypeInformation<?> actualType = typeInformation.getActualType();
245246

246247
// tuple-binding
247-
if (actualType != null && actualType.getType().isArray()) {
248+
if (actualType != null && actualType.getType().isArray() && !actualType.getType().equals(byte[].class)) {
248249

249250
TypeInformation<?> nestedElementType = actualType.getRequiredActualType();
250251
return writeCollection(collection, parameter.getActualSqlType(),

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

+58-13
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.junit.jupiter.params.ParameterizedTest;
4242
import org.junit.jupiter.params.provider.Arguments;
4343
import org.junit.jupiter.params.provider.MethodSource;
44-
4544
import org.springframework.beans.factory.annotation.Autowired;
4645
import org.springframework.beans.factory.config.PropertiesFactoryBean;
4746
import org.springframework.context.ApplicationListener;
@@ -51,23 +50,13 @@
5150
import org.springframework.core.io.ClassPathResource;
5251
import org.springframework.dao.IncorrectResultSizeDataAccessException;
5352
import org.springframework.data.annotation.Id;
54-
import org.springframework.data.domain.Example;
55-
import org.springframework.data.domain.ExampleMatcher;
56-
import org.springframework.data.domain.Limit;
57-
import org.springframework.data.domain.Page;
58-
import org.springframework.data.domain.PageRequest;
59-
import org.springframework.data.domain.Pageable;
60-
import org.springframework.data.domain.ScrollPosition;
61-
import org.springframework.data.domain.Slice;
62-
import org.springframework.data.domain.Sort;
63-
import org.springframework.data.domain.Window;
53+
import org.springframework.data.domain.*;
6454
import org.springframework.data.jdbc.core.mapping.AggregateReference;
6555
import org.springframework.data.jdbc.repository.query.Modifying;
6656
import org.springframework.data.jdbc.repository.query.Query;
6757
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
6858
import org.springframework.data.jdbc.testing.ConditionalOnDatabase;
6959
import org.springframework.data.jdbc.testing.DatabaseType;
70-
import org.springframework.data.jdbc.testing.EnabledOnDatabase;
7160
import org.springframework.data.jdbc.testing.EnabledOnFeature;
7261
import org.springframework.data.jdbc.testing.IntegrationTest;
7362
import org.springframework.data.jdbc.testing.TestConfiguration;
@@ -126,7 +115,6 @@ private static DummyEntity createDummyEntity(String entityName) {
126115

127116
DummyEntity entity = new DummyEntity();
128117
entity.setName(entityName);
129-
130118
return entity;
131119
}
132120

@@ -1357,6 +1345,52 @@ void queryWithTupleIn() {
13571345
assertThat(result).containsOnly(two);
13581346
}
13591347

1348+
@Test // GH-1900
1349+
void queryByListOfByteArray() {
1350+
1351+
byte[] oneBytes = { 1, 2, 3, 4, 5, 6, 7, 8 };
1352+
DummyEntity one = createDummyEntity("one");
1353+
one.setBytes(oneBytes);
1354+
one = repository.save(one);
1355+
1356+
byte[] twoBytes = { 8, 7, 6, 5, 4, 3, 2, 1 };
1357+
DummyEntity two = createDummyEntity("two");
1358+
two.setBytes(twoBytes);
1359+
two = repository.save(two);
1360+
1361+
byte[] threeBytes = { 3, 3, 3, 3, 3, 3, 3, 3 };
1362+
DummyEntity three = createDummyEntity("three");
1363+
three.setBytes(threeBytes);
1364+
three = repository.save(three);
1365+
1366+
List<DummyEntity> result = repository.findByBytesIn(List.of(threeBytes, oneBytes));
1367+
1368+
assertThat(result).extracting("idProp").containsExactlyInAnyOrder(one.idProp, three.idProp);
1369+
}
1370+
1371+
@Test // GH-1900
1372+
void queryByByteArray() {
1373+
1374+
byte[] oneBytes = { 1, 2, 3, 4, 5, 6, 7, 8 };
1375+
DummyEntity one = createDummyEntity("one");
1376+
one.setBytes(oneBytes);
1377+
one = repository.save(one);
1378+
1379+
byte[] twoBytes = { 8, 7, 6, 5, 4, 3, 2, 1 };
1380+
DummyEntity two = createDummyEntity("two");
1381+
two.setBytes(twoBytes);
1382+
two = repository.save(two);
1383+
1384+
byte[] threeBytes = { 3, 3, 3, 3, 3, 3, 3, 3 };
1385+
DummyEntity three = createDummyEntity("three");
1386+
three.setBytes(threeBytes);
1387+
three = repository.save(three);
1388+
1389+
List<DummyEntity> result = repository.findByBytes(twoBytes);
1390+
1391+
assertThat(result).extracting("idProp").containsExactly(two.idProp);
1392+
}
1393+
13601394
private Root createRoot(String namePrefix) {
13611395

13621396
return new Root(null, namePrefix,
@@ -1487,6 +1521,12 @@ interface DummyEntityRepository extends CrudRepository<DummyEntity, Long>, Query
14871521

14881522
@Query("SELECT * FROM DUMMY_ENTITY WHERE (ID_PROP, NAME) IN (:tuples)")
14891523
List<DummyEntity> findByListInTuple(List<Object[]> tuples);
1524+
1525+
@Query("SELECT * FROM DUMMY_ENTITY WHERE BYTES IN (:bytes)")
1526+
List<DummyEntity> findByBytesIn(List<byte[]> bytes);
1527+
1528+
@Query("SELECT * FROM DUMMY_ENTITY WHERE BYTES = :bytes")
1529+
List<DummyEntity> findByBytes(byte[] bytes);
14901530
}
14911531

14921532
interface RootRepository extends ListCrudRepository<Root, Long> {
@@ -1810,6 +1850,7 @@ static class DummyEntity {
18101850
boolean flag;
18111851
AggregateReference<DummyEntity, Long> ref;
18121852
Direction direction;
1853+
byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
18131854

18141855
public DummyEntity(String name) {
18151856
this.name = name;
@@ -1890,6 +1931,10 @@ public int hashCode() {
18901931
return Objects.hash(name, pointInTime, offsetDateTime, idProp, flag, ref, direction);
18911932
}
18921933

1934+
public void setBytes(byte[] bytes) {
1935+
this.bytes = bytes;
1936+
}
1937+
18931938
@Override
18941939
public String toString() {
18951940
return "DummyEntity{" + "name='" + name + '\'' + ", idProp=" + idProp + '}';

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/MsSqlDataSourceConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public MsSqlDataSourceConfiguration(TestClass testClass, Environment environment
4949
protected DataSource createDataSource() {
5050

5151
if (MSSQL_CONTAINER == null) {
52-
52+
System.out.println("------------------- creation Microsoft SQL Server container -----------------------------------");
5353
MSSQLServerContainer<?> container = new MSSQLServerContainer<>(MS_SQL_SERVER_VERSION) //
5454
.withReuse(true);
5555
container.start();

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ CREATE TABLE dummy_entity
1212
OFFSET_DATE_TIME TIMESTAMP, -- with time zone is only supported with z/OS
1313
FLAG BOOLEAN,
1414
REF BIGINT,
15-
DIRECTION VARCHAR(100)
15+
DIRECTION VARCHAR(100),
16+
BYTES BINARY(8)
1617
);
1718

1819
CREATE TABLE ROOT

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ CREATE TABLE dummy_entity
66
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
77
FLAG BOOLEAN,
88
REF BIGINT,
9-
DIRECTION VARCHAR(100)
9+
DIRECTION VARCHAR(100),
10+
BYTES BINARY(8)
1011
);
1112

1213
CREATE TABLE ROOT

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ CREATE TABLE dummy_entity
66
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
77
FLAG BOOLEAN,
88
REF BIGINT,
9-
DIRECTION VARCHAR(100)
9+
DIRECTION VARCHAR(100),
10+
BYTES BINARY(8)
1011
);
1112

1213
CREATE TABLE ROOT

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ CREATE TABLE dummy_entity
66
OFFSET_DATE_TIME TIMESTAMP(3),
77
FLAG BOOLEAN,
88
REF BIGINT,
9-
DIRECTION VARCHAR(100)
9+
DIRECTION VARCHAR(100),
10+
BYTES BINARY(8)
1011
);
1112

1213
CREATE TABLE ROOT

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ CREATE TABLE dummy_entity
1212
OFFSET_DATE_TIME DATETIMEOFFSET,
1313
FLAG BIT,
1414
REF BIGINT,
15-
DIRECTION VARCHAR(100)
15+
DIRECTION VARCHAR(100),
16+
BYTES VARBINARY(8)
1617
);
1718

1819
CREATE TABLE ROOT

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ CREATE TABLE DUMMY_ENTITY
99
OFFSET_DATE_TIME TIMESTAMP(3) DEFAULT NULL,
1010
FLAG BIT(1),
1111
REF BIGINT,
12-
DIRECTION VARCHAR(100)
12+
DIRECTION VARCHAR(100),
13+
BYTES BINARY(8)
1314
);
1415

1516
CREATE TABLE ROOT

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ CREATE TABLE DUMMY_ENTITY
1212
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
1313
FLAG NUMBER(1,0),
1414
REF NUMBER,
15-
DIRECTION VARCHAR2(100)
15+
DIRECTION VARCHAR2(100),
16+
BYTES RAW(8)
1617
);
1718

1819
CREATE TABLE ROOT

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ CREATE TABLE dummy_entity
1212
OFFSET_DATE_TIME TIMESTAMP WITH TIME ZONE,
1313
FLAG BOOLEAN,
1414
REF BIGINT,
15-
DIRECTION VARCHAR(100)
15+
DIRECTION VARCHAR(100),
16+
BYTES BYTEA
1617
);
1718

1819
CREATE TABLE ROOT

0 commit comments

Comments
 (0)