Skip to content

Commit 004804a

Browse files
committed
Introduce @InsertOnlyProperty.
You may now annotate properties of the aggregate root with `@InsertOnlyProperty`. Properties annotated in such way will be written to the database only during insert operations, but they will not be updated afterwards. Closes #637 Original pull request #1327
1 parent 76ea47a commit 004804a

File tree

17 files changed

+1582
-1354
lines changed

17 files changed

+1582
-1354
lines changed

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

+950-944
Large diffs are not rendered by default.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ <T> SqlIdentifierParameterSource forInsert(T instance, Class<T> domainType, Iden
9595
*/
9696
<T> SqlIdentifierParameterSource forUpdate(T instance, Class<T> domainType) {
9797

98-
return getParameterSource(instance, getRequiredPersistentEntity(domainType), "", Predicates.includeAll(),
98+
return getParameterSource(instance, getRequiredPersistentEntity(domainType), "", RelationalPersistentProperty::isInsertOnly,
9999
dialect.getIdentifierProcessing());
100100
}
101101

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.springframework.data.jdbc.testing.TestDatabaseFeatures;
6565
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
6666
import org.springframework.data.relational.core.mapping.Column;
67+
import org.springframework.data.relational.core.mapping.InsertOnlyProperty;
6768
import org.springframework.data.relational.core.mapping.MappedCollection;
6869
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
6970
import org.springframework.data.relational.core.mapping.Table;
@@ -1030,6 +1031,20 @@ void updateIdOnlyAggregate() {
10301031
template.save(entity);
10311032
}
10321033

1034+
@Test // GH-637
1035+
void insertOnlyPropertyDoesNotGetUpdated() {
1036+
1037+
WithInsertOnly entity = new WithInsertOnly();
1038+
entity.insertOnly = "first value";
1039+
1040+
assertThat(template.save(entity).id).isNotNull();
1041+
1042+
entity.insertOnly = "second value";
1043+
template.save(entity);
1044+
1045+
assertThat(template.findById(entity.id, WithInsertOnly.class).insertOnly).isEqualTo("first value");
1046+
}
1047+
10331048
private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
10341049
Function<Number, T> toConcreteNumber) {
10351050
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
@@ -1461,10 +1476,16 @@ static class WithLocalDateTime {
14611476
}
14621477

14631478
@Table
1464-
class WithIdOnly {
1479+
static class WithIdOnly {
14651480
@Id Long id;
14661481
}
14671482

1483+
@Table
1484+
static class WithInsertOnly {
1485+
@Id Long id;
1486+
@InsertOnlyProperty
1487+
String insertOnly;
1488+
}
14681489

14691490
@Configuration
14701491
@Import(TestConfiguration.class)

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

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ DROP TABLE WITH_LOCAL_DATE_TIME;
3939

4040
DROP TABLE WITH_ID_ONLY;
4141

42+
DROP TABLE WITH_INSERT_ONLY;
43+
4244
CREATE TABLE LEGO_SET
4345
(
4446
"id1" BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
@@ -357,4 +359,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
357359
CREATE TABLE WITH_ID_ONLY
358360
(
359361
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY
362+
);
363+
364+
CREATE TABLE WITH_INSERT_ONLY
365+
(
366+
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
367+
INSERT_ONLY VARCHAR(100)
360368
);

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

+6
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
326326
CREATE TABLE WITH_ID_ONLY
327327
(
328328
ID SERIAL PRIMARY KEY
329+
);
330+
331+
CREATE TABLE WITH_INSERT_ONLY
332+
(
333+
ID SERIAL PRIMARY KEY,
334+
INSERT_ONLY VARCHAR(100)
329335
);

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

+6
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
325325
TEST_TIME TIMESTAMP(9)
326326
);
327327

328+
CREATE TABLE WITH_INSERT_ONLY
329+
(
330+
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
331+
INSERT_ONLY VARCHAR(100)
332+
);
333+
328334
CREATE TABLE WITH_ID_ONLY
329335
(
330336
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY

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

+6
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
301301
CREATE TABLE WITH_ID_ONLY
302302
(
303303
ID BIGINT AUTO_INCREMENT PRIMARY KEY
304+
);
305+
306+
CREATE TABLE WITH_INSERT_ONLY
307+
(
308+
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
309+
INSERT_ONLY VARCHAR(100)
304310
);

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

+8
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,12 @@ DROP TABLE IF EXISTS WITH_ID_ONLY;
331331
CREATE TABLE WITH_ID_ONLY
332332
(
333333
ID BIGINT IDENTITY PRIMARY KEY
334+
);
335+
336+
DROP TABLE IF EXISTS WITH_INSERT_ONLY;
337+
338+
CREATE TABLE WITH_INSERT_ONLY
339+
(
340+
ID BIGINT IDENTITY PRIMARY KEY,
341+
INSERT_ONLY VARCHAR(100)
334342
);

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

+6
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
306306
CREATE TABLE WITH_ID_ONLY
307307
(
308308
ID BIGINT AUTO_INCREMENT PRIMARY KEY
309+
);
310+
311+
CREATE TABLE WITH_INSERT_ONLY
312+
(
313+
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
314+
INSERT_ONLY VARCHAR(100)
309315
);

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

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DROP TABLE VERSIONED_AGGREGATE CASCADE CONSTRAINTS PURGE;
2929
DROP TABLE WITH_READ_ONLY CASCADE CONSTRAINTS PURGE;
3030
DROP TABLE WITH_LOCAL_DATE_TIME CASCADE CONSTRAINTS PURGE;
3131
DROP TABLE WITH_ID_ONLY CASCADE CONSTRAINTS PURGE;
32+
DROP TABLE WITH_INSERT_ONLY CASCADE CONSTRAINTS PURGE;
3233

3334
CREATE TABLE LEGO_SET
3435
(
@@ -338,4 +339,11 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
338339
CREATE TABLE WITH_ID_ONLY
339340
(
340341
ID NUMBER GENERATED by default on null as IDENTITY PRIMARY KEY
342+
);
343+
344+
345+
CREATE TABLE WITH_INSERT_ONLY
346+
(
347+
ID NUMBER GENERATED by default on null as IDENTITY PRIMARY KEY,
348+
INSERT_ONLY VARCHAR(100)
341349
);

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

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ DROP TABLE CHAIN1;
1313
DROP TABLE CHAIN0;
1414
DROP TABLE WITH_READ_ONLY;
1515
DROP TABLE WITH_ID_ONLY;
16+
DROP TABLE WITH_INSERT_ONLY;
1617

1718
CREATE TABLE LEGO_SET
1819
(
@@ -341,4 +342,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
341342
CREATE TABLE WITH_ID_ONLY
342343
(
343344
ID SERIAL PRIMARY KEY
345+
);
346+
347+
CREATE TABLE WITH_INSERT_ONLY
348+
(
349+
ID SERIAL PRIMARY KEY,
350+
INSERT_ONLY VARCHAR(100)
344351
);

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplate.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.stream.Collectors;
3232

3333
import org.reactivestreams.Publisher;
34-
3534
import org.springframework.beans.BeansException;
3635
import org.springframework.beans.factory.BeanFactory;
3736
import org.springframework.beans.factory.BeanFactoryAware;
@@ -594,6 +593,13 @@ private <T> Mono<T> doUpdate(T entity, SqlIdentifier tableName) {
594593

595594
SqlIdentifier idColumn = persistentEntity.getRequiredIdProperty().getColumnName();
596595
Parameter id = outboundRow.remove(idColumn);
596+
597+
persistentEntity.forEach(p -> {
598+
if (p.isInsertOnly()) {
599+
outboundRow.remove(p.getColumnName());
600+
}
601+
});
602+
597603
Criteria criteria = Criteria.where(dataAccessStrategy.toSql(idColumn)).is(id);
598604

599605
if (matchingVersionCriteria != null) {

0 commit comments

Comments
 (0)