Skip to content

Introduce @InsertOnlyProperty. #1327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-637-insert-only-property-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-637-insert-only-property-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-637-insert-only-property-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-637-insert-only-property-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ static class Columns {
private final List<SqlIdentifier> idColumnNames = new ArrayList<>();
private final List<SqlIdentifier> nonIdColumnNames = new ArrayList<>();
private final Set<SqlIdentifier> readOnlyColumnNames = new HashSet<>();
private final Set<SqlIdentifier> insertOnlyColumnNames = new HashSet<>();
private final Set<SqlIdentifier> insertableColumns;
private final Set<SqlIdentifier> updatableColumns;

Expand All @@ -1045,6 +1046,7 @@ static class Columns {

updatable.removeAll(idColumnNames);
updatable.removeAll(readOnlyColumnNames);
updatable.removeAll(insertOnlyColumnNames);

this.updatableColumns = Collections.unmodifiableSet(updatable);
}
Expand Down Expand Up @@ -1077,6 +1079,10 @@ private void initSimpleColumnName(RelationalPersistentProperty property, String
if (!property.isWritable()) {
readOnlyColumnNames.add(columnName);
}

if (property.isInsertOnly()) {
insertOnlyColumnNames.add(columnName);
}
}

private void initEmbeddedColumnNames(RelationalPersistentProperty property, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <T> SqlIdentifierParameterSource forInsert(T instance, Class<T> domainType, Iden
*/
<T> SqlIdentifierParameterSource forUpdate(T instance, Class<T> domainType) {

return getParameterSource(instance, getRequiredPersistentEntity(domainType), "", Predicates.includeAll(),
return getParameterSource(instance, getRequiredPersistentEntity(domainType), "", RelationalPersistentProperty::isInsertOnly,
dialect.getIdentifierProcessing());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.springframework.data.jdbc.testing.TestDatabaseFeatures;
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.InsertOnlyProperty;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.Table;
Expand Down Expand Up @@ -1031,6 +1032,20 @@ void updateIdOnlyAggregate() {
template.save(entity);
}

@Test // GH-637
void insertOnlyPropertyDoesNotGetUpdated() {

WithInsertOnly entity = new WithInsertOnly();
entity.insertOnly = "first value";

assertThat(template.save(entity).id).isNotNull();

entity.insertOnly = "second value";
template.save(entity);

assertThat(template.findById(entity.id, WithInsertOnly.class).insertOnly).isEqualTo("first value");
}

private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
Function<Number, T> toConcreteNumber) {
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
Expand Down Expand Up @@ -1462,10 +1477,16 @@ static class WithLocalDateTime {
}

@Table
class WithIdOnly {
static class WithIdOnly {
@Id Long id;
}

@Table
static class WithInsertOnly {
@Id Long id;
@InsertOnlyProperty
String insertOnly;
}

@Configuration
@Import(TestConfiguration.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ DROP TABLE WITH_LOCAL_DATE_TIME;

DROP TABLE WITH_ID_ONLY;

DROP TABLE WITH_INSERT_ONLY;

CREATE TABLE LEGO_SET
(
"id1" BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
Expand Down Expand Up @@ -357,4 +359,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
CREATE TABLE WITH_ID_ONLY
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY
);

CREATE TABLE WITH_INSERT_ONLY
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
CREATE TABLE WITH_ID_ONLY
(
ID SERIAL PRIMARY KEY
);

CREATE TABLE WITH_INSERT_ONLY
(
ID SERIAL PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
TEST_TIME TIMESTAMP(9)
);

CREATE TABLE WITH_INSERT_ONLY
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);

CREATE TABLE WITH_ID_ONLY
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
CREATE TABLE WITH_ID_ONLY
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY
);

CREATE TABLE WITH_INSERT_ONLY
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,12 @@ DROP TABLE IF EXISTS WITH_ID_ONLY;
CREATE TABLE WITH_ID_ONLY
(
ID BIGINT IDENTITY PRIMARY KEY
);

DROP TABLE IF EXISTS WITH_INSERT_ONLY;

CREATE TABLE WITH_INSERT_ONLY
(
ID BIGINT IDENTITY PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
CREATE TABLE WITH_ID_ONLY
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY
);

CREATE TABLE WITH_INSERT_ONLY
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ DROP TABLE VERSIONED_AGGREGATE CASCADE CONSTRAINTS PURGE;
DROP TABLE WITH_READ_ONLY CASCADE CONSTRAINTS PURGE;
DROP TABLE WITH_LOCAL_DATE_TIME CASCADE CONSTRAINTS PURGE;
DROP TABLE WITH_ID_ONLY CASCADE CONSTRAINTS PURGE;
DROP TABLE WITH_INSERT_ONLY CASCADE CONSTRAINTS PURGE;

CREATE TABLE LEGO_SET
(
Expand Down Expand Up @@ -338,4 +339,11 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
CREATE TABLE WITH_ID_ONLY
(
ID NUMBER GENERATED by default on null as IDENTITY PRIMARY KEY
);


CREATE TABLE WITH_INSERT_ONLY
(
ID NUMBER GENERATED by default on null as IDENTITY PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ DROP TABLE CHAIN1;
DROP TABLE CHAIN0;
DROP TABLE WITH_READ_ONLY;
DROP TABLE WITH_ID_ONLY;
DROP TABLE WITH_INSERT_ONLY;

CREATE TABLE LEGO_SET
(
Expand Down Expand Up @@ -341,4 +342,10 @@ CREATE TABLE WITH_LOCAL_DATE_TIME
CREATE TABLE WITH_ID_ONLY
(
ID SERIAL PRIMARY KEY
);

CREATE TABLE WITH_INSERT_ONLY
(
ID SERIAL PRIMARY KEY,
INSERT_ONLY VARCHAR(100)
);
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-637-insert-only-property-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-637-insert-only-property-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Loading