Polishing.

Fixes a tiny typo.
Comments and formatting to make the important parts more obvious.

Original pull request #624
This commit is contained in:
Jens Schauder
2021-05-07 10:48:04 +02:00
parent 744ee32425
commit c981f62aab
4 changed files with 31 additions and 39 deletions

View File

@@ -1,6 +1,6 @@
== Spring Data JDBC with Immutables == 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. 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. Persisting immutable objects and associating the saved object with generated identifiers works out of the box.

View File

@@ -82,25 +82,35 @@ class Application {
@Override @Override
public <T> T mapRow(RelationalPersistentEntity<T> entity, ResultSet resultSet, Object key) { public <T> T mapRow(RelationalPersistentEntity<T> 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<T> implementationEntity = getImplementationEntity(mappingContext, entity);
return super.mapRow(implementationEntity, resultSet, key);
} }
@Override @Override
public <T> T mapRow(PersistentPropertyPathExtension path, ResultSet resultSet, Identifier identifier, public <T> T mapRow(PersistentPropertyPathExtension path, ResultSet resultSet, Identifier identifier,
Object key) { Object key) {
return super.mapRow(new DelegatePersistentPropertyPathExtension(mappingContext, // rows will not mapped to the entity interface, but to its implementation generated by Immutable
path.getRequiredPersistentPropertyPath(), getImplementationEntity(mappingContext, path.getLeafEntity())), RelationalPersistentEntity<?> implementationEntity = getImplementationEntity(mappingContext,
resultSet, identifier, key); 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") @SuppressWarnings("unchecked")
private <T> RelationalPersistentEntity<T> getImplementationEntity(JdbcMappingContext mappingContext, private <T> RelationalPersistentEntity<T> getImplementationEntity(JdbcMappingContext mappingContext,
RelationalPersistentEntity<T> entity) { RelationalPersistentEntity<T> entity) {
var type = entity.getType(); Class<T> type = entity.getType();
if (type.isInterface()) { if (type.isInterface()) {
var immutableClass = String.format(IMMUTABLE_IMPLEMENTATION_CLASS, type.getPackageName(), type.getSimpleName()); var immutableClass = String.format(IMMUTABLE_IMPLEMENTATION_CLASS, type.getPackageName(), type.getSimpleName());
@@ -125,7 +135,9 @@ class Application {
public DelegatePersistentPropertyPathExtension( public DelegatePersistentPropertyPathExtension(
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context, MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
PersistentPropertyPath<? extends RelationalPersistentProperty> path, RelationalPersistentEntity<?> leafEntity) { PersistentPropertyPath<? extends RelationalPersistentProperty> path, RelationalPersistentEntity<?> leafEntity) {
super(context, path); super(context, path);
this.leafEntity = leafEntity; this.leafEntity = leafEntity;
} }

View File

@@ -36,10 +36,10 @@ public interface Rotor {
* Factory method for {@link Rotor} as using {@code @Value.Style} and {@code @Value.Parameter} conflicts with Spring * Factory method for {@link Rotor} as using {@code @Value.Style} and {@code @Value.Parameter} conflicts with Spring
* Data's constructor discovery rules. * Data's constructor discovery rules.
* *
* @param name * @param name The name of the rotor, just a label to distinguish them.
* @param wiring * @param wiring A String consisting of all letters of the alphabet encoding which input letter is connected to which output letter.
* @param notch * @param notch The current position of the rotor.
* @return * @return a newly created Rotor.
*/ */
static Rotor of(String name, String wiring, char notch) { static Rotor of(String name, String wiring, char notch) {
return ImmutableRotor.builder().name(name).wiring(wiring).notch(notch).build(); return ImmutableRotor.builder().name(name).wiring(wiring).notch(notch).build();

View File

@@ -1,34 +1,14 @@
CREATE TABLE IF NOT EXISTS ENIGMA CREATE TABLE IF NOT EXISTS ENIGMA
( (
ID ID INTEGER IDENTITY PRIMARY KEY,
INTEGER MODEL VARCHAR(100)
IDENTITY );
PRIMARY
KEY,
MODEL
VARCHAR
(
100
)
);
CREATE TABLE IF NOT EXISTS ROTOR CREATE TABLE IF NOT EXISTS ROTOR
( (
ENIGMA_ID ENIGMA_ID INTEGER,
INTEGER, ROTOR_KEY INTEGER,
ROTOR_KEY NAME VARCHAR(100),
INTEGER, WIRING VARCHAR(26),
NAME NOTCH CHAR(1)
VARCHAR );
(
100
),
WIRING VARCHAR
(
26
),
NOTCH CHAR
(
1
)
);