diff --git a/jdbc/immutables/README.adoc b/jdbc/immutables/README.adoc index 3553ccc2..dcc5a1b9 100644 --- a/jdbc/immutables/README.adoc +++ b/jdbc/immutables/README.adoc @@ -1,6 +1,6 @@ == Spring Data JDBC with Immutables -This example show how to use https://immutables.github.io/[Immutables] with Spring Data. +This example shows how to use https://immutables.github.io/[Immutables] with Spring Data. The core concept of Immutables is to define an interface (or abstract class) for which Immutables generates an immutable implementation that can then be used by application code. Persisting immutable objects and associating the saved object with generated identifiers works out of the box. diff --git a/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java b/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java index 073c3c3b..553b545f 100644 --- a/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java +++ b/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Application.java @@ -82,25 +82,35 @@ class Application { @Override public T mapRow(RelationalPersistentEntity entity, ResultSet resultSet, Object key) { - return super.mapRow(getImplementationEntity(mappingContext, entity), resultSet, key); + + // rows will not mapped to the entity interface, but to its implementation generated by Immutable + RelationalPersistentEntity implementationEntity = getImplementationEntity(mappingContext, entity); + return super.mapRow(implementationEntity, resultSet, key); } @Override public T mapRow(PersistentPropertyPathExtension path, ResultSet resultSet, Identifier identifier, Object key) { - return super.mapRow(new DelegatePersistentPropertyPathExtension(mappingContext, - path.getRequiredPersistentPropertyPath(), getImplementationEntity(mappingContext, path.getLeafEntity())), - resultSet, identifier, key); + // rows will not mapped to the entity interface, but to its implementation generated by Immutable + RelationalPersistentEntity implementationEntity = getImplementationEntity(mappingContext, + path.getLeafEntity()); + var propertyPath = new DelegatePersistentPropertyPathExtension(mappingContext, + path.getRequiredPersistentPropertyPath(), implementationEntity); + return super.mapRow(propertyPath, resultSet, identifier, key); } }; } + /** + * Returns if the entity passed as an argument is an interface the implementation provided by Immutable is provided + * instead. In all other cases the entity passed as an argument is returned. + */ @SuppressWarnings("unchecked") private RelationalPersistentEntity getImplementationEntity(JdbcMappingContext mappingContext, RelationalPersistentEntity entity) { - var type = entity.getType(); + Class type = entity.getType(); if (type.isInterface()) { var immutableClass = String.format(IMMUTABLE_IMPLEMENTATION_CLASS, type.getPackageName(), type.getSimpleName()); @@ -125,7 +135,9 @@ class Application { public DelegatePersistentPropertyPathExtension( MappingContext, ? extends RelationalPersistentProperty> context, PersistentPropertyPath path, RelationalPersistentEntity leafEntity) { + super(context, path); + this.leafEntity = leafEntity; } diff --git a/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Rotor.java b/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Rotor.java index 6b9a25b5..6f71ebd1 100644 --- a/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Rotor.java +++ b/jdbc/immutables/src/main/java/example/springdata/jdbc/immutables/Rotor.java @@ -36,10 +36,10 @@ public interface Rotor { * Factory method for {@link Rotor} as using {@code @Value.Style} and {@code @Value.Parameter} conflicts with Spring * Data's constructor discovery rules. * - * @param name - * @param wiring - * @param notch - * @return + * @param name The name of the rotor, just a label to distinguish them. + * @param wiring A String consisting of all letters of the alphabet encoding which input letter is connected to which output letter. + * @param notch The current position of the rotor. + * @return a newly created Rotor. */ static Rotor of(String name, String wiring, char notch) { return ImmutableRotor.builder().name(name).wiring(wiring).notch(notch).build(); diff --git a/jdbc/immutables/src/main/resources/schema.sql b/jdbc/immutables/src/main/resources/schema.sql index 1b397e27..7a5faaf8 100644 --- a/jdbc/immutables/src/main/resources/schema.sql +++ b/jdbc/immutables/src/main/resources/schema.sql @@ -1,34 +1,14 @@ CREATE TABLE IF NOT EXISTS ENIGMA ( - ID - INTEGER - IDENTITY - PRIMARY - KEY, - MODEL - VARCHAR -( - 100 -) - ); + ID INTEGER IDENTITY PRIMARY KEY, + MODEL VARCHAR(100) +); CREATE TABLE IF NOT EXISTS ROTOR ( - ENIGMA_ID - INTEGER, - ROTOR_KEY - INTEGER, - NAME - VARCHAR -( - 100 -), - WIRING VARCHAR -( - 26 -), - NOTCH CHAR -( - 1 -) - ); + ENIGMA_ID INTEGER, + ROTOR_KEY INTEGER, + NAME VARCHAR(100), + WIRING VARCHAR(26), + NOTCH CHAR(1) +);