The `@ReadOnlyProperty` annotation is now honoured for references to entities or collections of entities.
For tables mapped to such annotated references, no insert, delete or update statements will be created.
The user has to maintain that data through some other means.
These could be triggers or external process or `ON DELETE CASCADE` configuration in the database schema.
Closes#1249
Original pull request #1250
@ -222,6 +224,17 @@ public class PersistentPropertyPathExtensionUnitTests {
@@ -222,6 +224,17 @@ public class PersistentPropertyPathExtensionUnitTests {
});
}
@Test// GH-1249
voidisWritable(){
assertSoftly(softly->{
softly.assertThat(PersistentPropertyPathExtension.isWritable(createSimplePath("withId"))).describedAs("simple path is writable").isTrue();
softly.assertThat(PersistentPropertyPathExtension.isWritable(createSimplePath("secondList.third2"))).describedAs("long path is writable").isTrue();
softly.assertThat(PersistentPropertyPathExtension.isWritable(createSimplePath("second"))).describedAs("simple read only path is not writable").isFalse();
softly.assertThat(PersistentPropertyPathExtension.isWritable(createSimplePath("second.third"))).describedAs("long path containing read only element is not writable").isFalse();
@ -72,8 +74,10 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl
@@ -72,8 +74,10 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl
@ -114,8 +118,10 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl
@@ -114,8 +118,10 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl
Spring Data JDBC uses the ID to identify entities.
The ID of an entity must be annotated with Spring Data's https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html[`@Id`] annotation.
When your database has an auto-increment column for the ID column, the generated value gets set in the entity after inserting it into the database.
When your database has an auto-increment column for the ID column, the generated value gets set in the entity after inserting it into the database.
One important constraint is that, after saving an entity, the entity must not be new any more.
Note that whether an entity is new is part of the entity's state.
With auto-increment columns, this happens automatically, because the ID gets set by Spring Data with the value from the ID column.
If you are not using auto-increment columns, you can use a `BeforeConvert` listener, which sets the ID of the entity (covered later in this document).
[[jdbc.entity-persistence.read-only-properties]]
=== Read Only Properties
Attributes annotated with `@ReadOnlyProperty` will not be written to the database by Spring Data JDBC, but they will be read when an entity gets loaded.
Spring Data JDBC will not automatically reload an entity after writing it.
Therefore, you have to reload it explicitly if you want to see data that was generated in the database for such columns.
If the annotated attribute is an entity or collection of entities, it is represented by one or more separate rows in separate tables.
Spring Data JDBC will not perform any insert, delete or update for these rows.