Skip to content

Commit a066d2f

Browse files
schaudermp911de
authored andcommitted
DATAJDBC-431 - ReadOnlyProperty now no longer written.
The problem was that the SqlGenerator honored the annotation but they were included as query parameters and therefore automatically added back again. Also: * Simplified the relevant filter in the SqlGenerator. * Introduced a meta annotation for running tests only agains HsqlDb. Original pull request: #175.
1 parent 3761b61 commit a066d2f

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private <S, T> MapSqlParameterSource getParameterSource(S instance, RelationalPe
323323

324324
persistentEntity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {
325325

326-
if (skipProperty.test(property)) {
326+
if (skipProperty.test(property) || !property.isWritable()) {
327327
return;
328328
}
329329
if (property.isEntity() && !property.isEmbedded()) {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.regex.Pattern;
3030
import java.util.stream.Collectors;
3131

32-
import org.springframework.data.annotation.ReadOnlyProperty;
3332
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
3433
import org.springframework.data.mapping.PersistentPropertyPath;
3534
import org.springframework.data.mapping.PropertyHandler;
@@ -623,7 +622,7 @@ private void initSimpleColumnName(RelationalPersistentProperty property, String
623622
idColumnNames.add(columnName);
624623
}
625624

626-
if (!property.isWritable() || property.isAnnotationPresent(ReadOnlyProperty.class)) {
625+
if (!property.isWritable()) {
627626
readOnlyColumnNames.add(columnName);
628627
}
629628
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java

+25
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import lombok.Data;
2222
import lombok.EqualsAndHashCode;
2323

24+
import java.util.AbstractMap;
2425
import java.util.ArrayList;
2526
import java.util.Arrays;
27+
import java.util.Collections;
2628
import java.util.HashMap;
2729
import java.util.HashSet;
2830
import java.util.List;
@@ -41,8 +43,10 @@
4143
import org.springframework.context.annotation.Configuration;
4244
import org.springframework.context.annotation.Import;
4345
import org.springframework.data.annotation.Id;
46+
import org.springframework.data.annotation.ReadOnlyProperty;
4447
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
4548
import org.springframework.data.jdbc.testing.DatabaseProfileValueSource;
49+
import org.springframework.data.jdbc.testing.HsqlDbOnly;
4650
import org.springframework.data.jdbc.testing.TestConfiguration;
4751
import org.springframework.data.relational.core.conversion.RelationalConverter;
4852
import org.springframework.data.relational.core.mapping.Column;
@@ -597,6 +601,20 @@ public void shouldDeleteChainOfMapsWithoutIds() {
597601
});
598602
}
599603

604+
@Test // DATAJDBC-431
605+
@HsqlDbOnly
606+
public void readOnlyGetsLoadedButNotWritten() {
607+
608+
WithReadOnly entity = new WithReadOnly();
609+
entity.name = "Alfred";
610+
entity.readOnly = "not used";
611+
612+
template.save(entity);
613+
614+
assertThat(
615+
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class)).isEqualTo("from-db");
616+
}
617+
600618
private static NoIdMapChain4 createNoIdMapTree() {
601619

602620
NoIdMapChain4 chain4 = new NoIdMapChain4();
@@ -855,6 +873,13 @@ static class NoIdMapChain4 {
855873
Map<String, NoIdMapChain3> chain3 = new HashMap<>();
856874
}
857875

876+
static class WithReadOnly {
877+
@Id Long id;
878+
String name;
879+
@ReadOnlyProperty
880+
String readOnly;
881+
}
882+
858883
@Configuration
859884
@Import(TestConfiguration.class)
860885
static class Config {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.testing;
17+
18+
import org.springframework.test.annotation.IfProfileValue;
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Inherited;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Run the annotated test only against a HsqlDb database.
29+
*
30+
* Requires the use of
31+
*
32+
* @author Jens Schauder
33+
*/
34+
@Target({ElementType.TYPE, ElementType.METHOD})
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Documented
37+
@Inherited
38+
@IfProfileValue(name = "current.database.is.not.hsqldb", value = "false")
39+
public @interface HsqlDbOnly {
40+
}

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-hsql.sql

+7-1
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,10 @@ CREATE TABLE NO_ID_MAP_CHAIN0
292292
NO_ID_MAP_CHAIN3_KEY,
293293
NO_ID_MAP_CHAIN2_KEY
294294
)
295-
);
295+
);
296+
297+
CREATE TABLE WITH_READ_ONLY (
298+
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 40) PRIMARY KEY,
299+
NAME VARCHAR(200),
300+
READ_ONLY VARCHAR(200) DEFAULT 'from-db'
301+
)

0 commit comments

Comments
 (0)