Skip to content

Commit fca2ef6

Browse files
committed
DATAJDBC-137 - Improve EntityInformation usage.
Moved to both the usage of the newly introduced PersistentEntity.isNew(…) and identifier lookups via PersistentEntity instead of using a custom EntityInformation implementation. JdbcRepositoryFactory now creates a PersistentEntityInformation, SimpleJdbcRepository simply works with a PersistentEntity. Removed references to EntityInformation (a repository subsystem concept) from the template implementation. Removed BasicJdbcPersistentEntity and its tests entirely. JdbcAuditingEventListener is now using an IsNewAwareAuditingHandler. Related tickets: DATACMNS-1333.
1 parent 6bf916b commit fca2ef6

File tree

12 files changed

+79
-283
lines changed

12 files changed

+79
-283
lines changed

src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727
import org.springframework.dao.EmptyResultDataAccessException;
2828
import org.springframework.dao.InvalidDataAccessApiUsageException;
2929
import org.springframework.dao.NonTransientDataAccessException;
30-
import org.springframework.data.jdbc.core.mapping.BasicJdbcPersistentEntityInformation;
3130
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
3231
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntity;
33-
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntityInformation;
3432
import org.springframework.data.jdbc.core.mapping.JdbcPersistentProperty;
3533
import org.springframework.data.jdbc.support.JdbcUtil;
34+
import org.springframework.data.mapping.PersistentPropertyAccessor;
3635
import org.springframework.data.mapping.PropertyHandler;
3736
import org.springframework.data.mapping.PropertyPath;
38-
import org.springframework.data.repository.core.EntityInformation;
37+
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
3938
import org.springframework.jdbc.core.RowMapper;
4039
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
4140
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
@@ -58,8 +57,8 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
5857

5958
private final @NonNull SqlGeneratorSource sqlGeneratorSource;
6059
private final @NonNull JdbcMappingContext context;
61-
private final @NonNull DataAccessStrategy accessStrategy;
6260
private final @NonNull NamedParameterJdbcOperations operations;
61+
private final @NonNull DataAccessStrategy accessStrategy;
6362

6463
/**
6564
* Creates a {@link DefaultDataAccessStrategy} which references it self for resolution of recursive data accesses.
@@ -79,8 +78,6 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
7978

8079
KeyHolder holder = new GeneratedKeyHolder();
8180
JdbcPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
82-
JdbcPersistentEntityInformation<T, ?> entityInformation = context
83-
.getRequiredPersistentEntityInformation(domainType);
8481

8582
MapSqlParameterSource parameterSource = getPropertyMap(instance, persistentEntity);
8683

@@ -103,7 +100,8 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
103100

104101
// if there is an id property and it was null before the save
105102
// The database should have created an id and provided it.
106-
if (idProperty != null && idValue == null && entityInformation.isNew(instance)) {
103+
104+
if (idProperty != null && idValue == null && persistentEntity.isNew(instance)) {
107105
throw new IllegalStateException(String.format(ENTITY_NEW_AFTER_INSERT, persistentEntity));
108106
}
109107
}
@@ -278,15 +276,12 @@ private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersisten
278276
@SuppressWarnings("unchecked")
279277
private <S, ID> ID getIdValueOrNull(S instance, JdbcPersistentEntity<S> persistentEntity) {
280278

281-
EntityInformation<S, ID> entityInformation = (EntityInformation<S, ID>) context
282-
.getRequiredPersistentEntityInformation(persistentEntity.getType());
283-
284-
ID idValue = entityInformation.getId(instance);
279+
ID idValue = (ID) persistentEntity.getIdentifierAccessor(instance).getIdentifier();
285280

286281
return isIdPropertyNullOrScalarZero(idValue, persistentEntity) ? null : idValue;
287282
}
288283

289-
private <S, ID> boolean isIdPropertyNullOrScalarZero(ID idValue, JdbcPersistentEntity<S> persistentEntity) {
284+
private static <S, ID> boolean isIdPropertyNullOrScalarZero(ID idValue, JdbcPersistentEntity<S> persistentEntity) {
290285

291286
JdbcPersistentProperty idProperty = persistentEntity.getIdProperty();
292287
return idValue == null //
@@ -297,16 +292,16 @@ private <S, ID> boolean isIdPropertyNullOrScalarZero(ID idValue, JdbcPersistentE
297292

298293
private <S> void setIdFromJdbc(S instance, KeyHolder holder, JdbcPersistentEntity<S> persistentEntity) {
299294

300-
JdbcPersistentEntityInformation<S, ?> entityInformation = new BasicJdbcPersistentEntityInformation<>(
301-
persistentEntity);
302-
303295
try {
304296

305297
getIdFromHolder(holder, persistentEntity).ifPresent(it -> {
306298

307-
Class<?> targetType = persistentEntity.getRequiredIdProperty().getType();
308-
Object converted = convert(it, targetType);
309-
entityInformation.setId(instance, converted);
299+
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);
300+
ConvertingPropertyAccessor convertingPropertyAccessor = new ConvertingPropertyAccessor(accessor,
301+
context.getConversions());
302+
JdbcPersistentProperty idProperty = persistentEntity.getRequiredIdProperty();
303+
304+
convertingPropertyAccessor.setProperty(idProperty, it);
310305
});
311306

312307
} catch (NonTransientDataAccessException e) {

src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
import org.springframework.data.jdbc.core.conversion.JdbcEntityDeleteWriter;
2525
import org.springframework.data.jdbc.core.conversion.JdbcEntityWriter;
2626
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
27-
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntityInformation;
27+
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntity;
2828
import org.springframework.data.jdbc.core.mapping.event.AfterDeleteEvent;
2929
import org.springframework.data.jdbc.core.mapping.event.AfterLoadEvent;
3030
import org.springframework.data.jdbc.core.mapping.event.AfterSaveEvent;
3131
import org.springframework.data.jdbc.core.mapping.event.BeforeDeleteEvent;
3232
import org.springframework.data.jdbc.core.mapping.event.BeforeSaveEvent;
3333
import org.springframework.data.jdbc.core.mapping.event.Identifier;
3434
import org.springframework.data.jdbc.core.mapping.event.Identifier.Specified;
35+
import org.springframework.data.mapping.IdentifierAccessor;
36+
import org.springframework.util.Assert;
3537

3638
/**
3739
* {@link JdbcAggregateOperations} implementation, storing aggregates in and obtaining them from a JDBC data store.
@@ -52,7 +54,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
5254
private final DataAccessStrategy accessStrategy;
5355

5456
public JdbcAggregateTemplate(ApplicationEventPublisher publisher, JdbcMappingContext context,
55-
DataAccessStrategy dataAccessStrategy) {
57+
DataAccessStrategy dataAccessStrategy) {
5658

5759
this.publisher = publisher;
5860
this.context = context;
@@ -66,21 +68,23 @@ public JdbcAggregateTemplate(ApplicationEventPublisher publisher, JdbcMappingCon
6668
@Override
6769
public <T> void save(T instance) {
6870

69-
JdbcPersistentEntityInformation<T, ?> entityInformation = context
70-
.getRequiredPersistentEntityInformation((Class<T>) instance.getClass());
71+
Assert.notNull(instance, "Agggregate instance must not be null!");
72+
73+
JdbcPersistentEntity<?> entity = context.getRequiredPersistentEntity(instance.getClass());
74+
IdentifierAccessor identifierAccessor = entity.getIdentifierAccessor(instance);
7175

7276
AggregateChange change = createChange(instance);
7377

7478
publisher.publishEvent(new BeforeSaveEvent( //
75-
Identifier.ofNullable(entityInformation.getId(instance)), //
79+
Identifier.ofNullable(identifierAccessor.getIdentifier()), //
7680
instance, //
7781
change //
7882
));
7983

8084
change.executeWith(interpreter);
8185

8286
publisher.publishEvent(new AfterSaveEvent( //
83-
Identifier.of(entityInformation.getId(instance)), //
87+
Identifier.of(identifierAccessor.getIdentifier()), //
8488
instance, //
8589
change //
8690
));
@@ -125,9 +129,10 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
125129
@Override
126130
public <S> void delete(S entity, Class<S> domainType) {
127131

128-
JdbcPersistentEntityInformation<S, ?> entityInformation = context
129-
.getRequiredPersistentEntityInformation(domainType);
130-
deleteTree(entityInformation.getRequiredId(entity), entity, domainType);
132+
IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType)
133+
.getIdentifierAccessor(entity);
134+
135+
deleteTree(identifierAccessor.getRequiredIdentifier(), entity, domainType);
131136
}
132137

133138
@Override
@@ -178,11 +183,14 @@ private AggregateChange createDeletingChange(Class<?> domainType) {
178183
return aggregateChange;
179184
}
180185

181-
@SuppressWarnings("unchecked")
182186
private <T> void publishAfterLoad(Iterable<T> all) {
183187

184188
for (T e : all) {
185-
publishAfterLoad(context.getRequiredPersistentEntityInformation((Class<T>) e.getClass()).getRequiredId(e), e);
189+
190+
JdbcPersistentEntity<?> entity = context.getPersistentEntity(e.getClass());
191+
IdentifierAccessor identifierAccessor = entity.getIdentifierAccessor(e);
192+
193+
publishAfterLoad(identifierAccessor.getRequiredIdentifier(), e);
186194
}
187195
}
188196

src/main/java/org/springframework/data/jdbc/core/conversion/JdbcEntityWriter.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import org.springframework.data.jdbc.core.conversion.DbAction.Update;
2828
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
2929
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntity;
30-
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntityInformation;
3130
import org.springframework.data.jdbc.core.mapping.JdbcPersistentProperty;
31+
import org.springframework.data.mapping.IdentifierAccessor;
32+
import org.springframework.data.mapping.PersistentEntity;
3233
import org.springframework.data.mapping.PersistentProperty;
3334
import org.springframework.data.mapping.PersistentPropertyAccessor;
3435
import org.springframework.data.util.StreamUtils;
35-
import org.springframework.util.ClassUtils;
3636

3737
/**
3838
* Converts an entity that is about to be saved into {@link DbAction}s inside a {@link AggregateChange} that need to be
@@ -43,8 +43,13 @@
4343
*/
4444
public class JdbcEntityWriter extends JdbcEntityWriterSupport {
4545

46+
private final JdbcMappingContext context;
47+
4648
public JdbcEntityWriter(JdbcMappingContext context) {
49+
4750
super(context);
51+
52+
this.context = context;
4853
}
4954

5055
@Override
@@ -54,11 +59,12 @@ public void write(Object o, AggregateChange aggregateChange) {
5459

5560
private void write(Object o, AggregateChange aggregateChange, DbAction dependingOn) {
5661

57-
Class<Object> type = (Class<Object>) o.getClass();
58-
JdbcPersistentEntityInformation<Object, ?> entityInformation = context.getRequiredPersistentEntityInformation(type);
62+
Class<?> type = o.getClass();
5963
JdbcPropertyPath propertyPath = JdbcPropertyPath.from("", type);
6064

61-
if (entityInformation.isNew(o)) {
65+
PersistentEntity<?, JdbcPersistentProperty> persistentEntity = context.getRequiredPersistentEntity(type);
66+
67+
if (persistentEntity.isNew(o)) {
6268

6369
Insert<Object> insert = DbAction.insert(o, propertyPath, dependingOn);
6470
aggregateChange.addAction(insert);
@@ -71,10 +77,13 @@ private void write(Object o, AggregateChange aggregateChange, DbAction depending
7177
aggregateChange, //
7278
propertyPath.nested(propertyAndValue.property.getName()), //
7379
insert) //
74-
);
80+
);
7581
} else {
7682

77-
deleteReferencedEntities(entityInformation.getRequiredId(o), aggregateChange);
83+
JdbcPersistentEntity<?> entity = context.getPersistentEntity(type);
84+
IdentifierAccessor identifierAccessor = entity.getIdentifierAccessor(o);
85+
86+
deleteReferencedEntities(identifierAccessor.getRequiredIdentifier(), aggregateChange);
7887

7988
Update<Object> update = DbAction.update(o, propertyPath, dependingOn);
8089
aggregateChange.addAction(update);
@@ -104,7 +113,7 @@ private void insertReferencedEntities(PropertyAndValue propertyAndValue, Aggrega
104113
aggregateChange, //
105114
propertyPath.nested(pav.property.getName()), //
106115
dependingOn) //
107-
);
116+
);
108117
}
109118

110119
private Stream<PropertyAndValue> referencedEntities(Object o) {
@@ -116,7 +125,7 @@ private Stream<PropertyAndValue> referencedEntities(Object o) {
116125
.flatMap( //
117126
p -> referencedEntity(p, persistentEntity.getPropertyAccessor(o)) //
118127
.map(e -> new PropertyAndValue(p, e)) //
119-
);
128+
);
120129
}
121130

122131
private Stream<Object> referencedEntity(JdbcPersistentProperty p, PersistentPropertyAccessor propertyAccessor) {

src/main/java/org/springframework/data/jdbc/core/mapping/BasicJdbcPersistentEntityInformation.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/main/java/org/springframework/data/jdbc/core/mapping/JdbcMappingContext.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ protected JdbcPersistentProperty createPersistentProperty(Property property, Jdb
139139
return new BasicJdbcPersistentProperty(property, owner, simpleTypeHolder, this);
140140
}
141141

142-
@SuppressWarnings("unchecked")
143-
public <T> JdbcPersistentEntityInformation<T, ?> getRequiredPersistentEntityInformation(Class<T> type) {
144-
return new BasicJdbcPersistentEntityInformation<>((JdbcPersistentEntity<T>) getRequiredPersistentEntity(type));
145-
}
146-
147142
public ConversionService getConversions() {
148143
return conversions;
149144
}

src/main/java/org/springframework/data/jdbc/core/mapping/JdbcPersistentEntityInformation.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)