Skip to content

Commit e87a41c

Browse files
committed
DATAJDBC-431 - ReadOnlyProperty now gets properly handled.
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.
1 parent 99b1fbd commit e87a41c

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 26 additions & 0 deletions
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,6 +43,7 @@
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;
4649
import org.springframework.data.jdbc.testing.TestConfiguration;
@@ -597,6 +600,22 @@ public void shouldDeleteChainOfMapsWithoutIds() {
597600
});
598601
}
599602

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

602621
NoIdMapChain4 chain4 = new NoIdMapChain4();
@@ -855,6 +874,13 @@ static class NoIdMapChain4 {
855874
Map<String, NoIdMapChain3> chain3 = new HashMap<>();
856875
}
857876

877+
static class WithReadOnly {
878+
@Id Long id;
879+
String name;
880+
@ReadOnlyProperty
881+
String readOnly;
882+
}
883+
858884
@Configuration
859885
@Import(TestConfiguration.class)
860886
static class Config {

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

Lines changed: 7 additions & 1 deletion
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)