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
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.

View File

@@ -82,25 +82,35 @@ class Application {
@Override
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
public <T> 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 <T> RelationalPersistentEntity<T> getImplementationEntity(JdbcMappingContext mappingContext,
RelationalPersistentEntity<T> entity) {
var type = entity.getType();
Class<T> 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 RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
PersistentPropertyPath<? extends RelationalPersistentProperty> path, RelationalPersistentEntity<?> leafEntity) {
super(context, path);
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
* 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();

View File

@@ -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)
);