|
2 | 2 |
|
3 | 3 | import java.util.Map;
|
4 | 4 | import java.util.Optional;
|
| 5 | + |
5 | 6 | import org.apache.commons.logging.Log;
|
6 | 7 | import org.apache.commons.logging.LogFactory;
|
7 | 8 | import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
|
| 9 | +import org.springframework.data.mapping.Parameter; |
8 | 10 | import org.springframework.data.mapping.PersistentPropertyAccessor;
|
| 11 | +import org.springframework.data.mapping.context.MappingContext; |
| 12 | +import org.springframework.data.mapping.model.ParameterValueProvider; |
9 | 13 | import org.springframework.data.relational.core.conversion.MutableAggregateChange;
|
| 14 | +import org.springframework.data.relational.core.conversion.RelationalConverter; |
10 | 15 | import org.springframework.data.relational.core.dialect.Dialect;
|
11 |
| -import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
12 | 16 | import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
| 17 | +import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
13 | 18 | import org.springframework.data.relational.core.mapping.event.BeforeSaveCallback;
|
14 | 19 | import org.springframework.data.relational.core.sql.SqlIdentifier;
|
15 | 20 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
| 21 | +import org.springframework.lang.Nullable; |
16 | 22 | import org.springframework.util.Assert;
|
17 | 23 |
|
18 | 24 | /**
|
19 |
| - * Callback for generating ID via the database sequence. By default, it is registered as a |
20 |
| - * bean in {@link AbstractJdbcConfiguration} |
| 25 | + * Callback for generating ID via the database sequence. By default, it is registered as a bean in |
| 26 | + * {@link AbstractJdbcConfiguration} |
21 | 27 | *
|
22 | 28 | * @author Mikhail Polivakha
|
23 | 29 | */
|
24 | 30 | public class IdGeneratingBeforeSaveCallback implements BeforeSaveCallback<Object> {
|
25 | 31 |
|
26 |
| - private static final Log LOG = LogFactory.getLog(IdGeneratingBeforeSaveCallback.class); |
27 |
| - |
28 |
| - private final RelationalMappingContext relationalMappingContext; |
29 |
| - private final Dialect dialect; |
30 |
| - private final NamedParameterJdbcOperations operations; |
31 |
| - |
32 |
| - public IdGeneratingBeforeSaveCallback( |
33 |
| - RelationalMappingContext relationalMappingContext, |
34 |
| - Dialect dialect, |
35 |
| - NamedParameterJdbcOperations namedParameterJdbcOperations |
36 |
| - ) { |
37 |
| - this.relationalMappingContext = relationalMappingContext; |
38 |
| - this.dialect = dialect; |
39 |
| - this.operations = namedParameterJdbcOperations; |
40 |
| - } |
41 |
| - |
42 |
| - @Override |
43 |
| - public Object onBeforeSave(Object aggregate, MutableAggregateChange<Object> aggregateChange) { |
44 |
| - |
45 |
| - Assert.notNull(aggregate, "The aggregate cannot be null at this point"); |
46 |
| - |
47 |
| - RelationalPersistentEntity<?> persistentEntity = relationalMappingContext.getPersistentEntity(aggregate.getClass()); |
48 |
| - Optional<SqlIdentifier> idSequence = persistentEntity.getIdSequence(); |
49 |
| - |
50 |
| - if (dialect.getIdGeneration().sequencesSupported()) { |
51 |
| - |
52 |
| - if (persistentEntity.getIdProperty() != null) { |
53 |
| - idSequence |
54 |
| - .map(s -> dialect.getIdGeneration().createSequenceQuery(s)) |
55 |
| - .ifPresent(sql -> { |
56 |
| - Long idValue = operations.queryForObject(sql, Map.of(), (rs, rowNum) -> rs.getLong(1)); |
57 |
| - PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(aggregate); |
58 |
| - propertyAccessor.setProperty(persistentEntity.getRequiredIdProperty(), idValue); |
59 |
| - }); |
60 |
| - } |
61 |
| - } else { |
62 |
| - if (idSequence.isPresent()) { |
63 |
| - LOG.warn(""" |
64 |
| - It seems you're trying to insert an aggregate of type '%s' annotated with @TargetSequence, but the problem is RDBMS you're |
65 |
| - working with does not support sequences as such. Falling back to identity columns |
66 |
| - """ |
67 |
| - .formatted(aggregate.getClass().getName()) |
68 |
| - ); |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - return aggregate; |
73 |
| - } |
| 32 | + private static final Log LOG = LogFactory.getLog(IdGeneratingBeforeSaveCallback.class); |
| 33 | + |
| 34 | + private final MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> relationalMappingContext; |
| 35 | + private final Dialect dialect; |
| 36 | + private final NamedParameterJdbcOperations operations; |
| 37 | + private final RelationalConverter converter; |
| 38 | + |
| 39 | + public IdGeneratingBeforeSaveCallback(Dialect dialect, NamedParameterJdbcOperations namedParameterJdbcOperations, |
| 40 | + RelationalConverter converter) { |
| 41 | + |
| 42 | + this.relationalMappingContext = converter.getMappingContext(); |
| 43 | + this.dialect = dialect; |
| 44 | + this.operations = namedParameterJdbcOperations; |
| 45 | + this.converter = converter; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Object onBeforeSave(Object aggregate, MutableAggregateChange<Object> aggregateChange) { |
| 50 | + |
| 51 | + Assert.notNull(aggregate, "The aggregate must not be null at this point"); |
| 52 | + |
| 53 | + RelationalPersistentEntity<?> persistentEntity = relationalMappingContext |
| 54 | + .getRequiredPersistentEntity(aggregate.getClass()); |
| 55 | + Optional<SqlIdentifier> idSequence = persistentEntity.getIdSequence(); |
| 56 | + |
| 57 | + if (dialect.getIdGeneration().sequencesSupported()) { |
| 58 | + |
| 59 | + if (persistentEntity.hasIdProperty()) { |
| 60 | + |
| 61 | + PersistentPropertyAccessor<Object> accessor = persistentEntity.getPropertyAccessor(aggregate); |
| 62 | + |
| 63 | + idSequence.map(this::querySequence).ifPresent(idValue -> { |
| 64 | + RelationalPersistentProperty idProperty = persistentEntity.getRequiredIdProperty(); |
| 65 | + if (idProperty.isEmbedded()) { |
| 66 | + |
| 67 | + setEmbeddedIdValue(persistentEntity, idProperty, aggregate, idValue, accessor); |
| 68 | + |
| 69 | + } else { |
| 70 | + accessor.setProperty(idProperty, idValue); |
| 71 | + } |
| 72 | + |
| 73 | + }); |
| 74 | + return accessor.getBean(); |
| 75 | + } |
| 76 | + } else { |
| 77 | + if (idSequence.isPresent()) { |
| 78 | + LOG.warn( |
| 79 | + """ |
| 80 | + It seems you're trying to insert an aggregate of type '%s' annotated with @TargetSequence, but the problem is RDBMS you're |
| 81 | + working with does not support sequences as such. Falling back to identity columns |
| 82 | + """ |
| 83 | + .formatted(aggregate.getClass().getName())); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return aggregate; |
| 88 | + } |
| 89 | + |
| 90 | + private void setEmbeddedIdValue(RelationalPersistentEntity<?> persistentEntity, |
| 91 | + RelationalPersistentProperty idProperty, Object aggregate, Long idValue, |
| 92 | + PersistentPropertyAccessor<Object> accessor) { |
| 93 | + |
| 94 | + Class<?> idPropertyType = idProperty.getType(); |
| 95 | + RelationalPersistentEntity<?> idEntity = relationalMappingContext.getRequiredPersistentEntity(idPropertyType); |
| 96 | + |
| 97 | + RelationalPersistentProperty[] propertyHolder = new RelationalPersistentProperty[1]; |
| 98 | + idEntity.doWithProperties((RelationalPersistentProperty property) -> { |
| 99 | + if (propertyHolder[0] != null) { |
| 100 | + throw new IllegalStateException( |
| 101 | + "There is no unique id property path for %s".formatted(persistentEntity.toString())); |
| 102 | + } |
| 103 | + propertyHolder[0] = property; |
| 104 | + }); |
| 105 | + |
| 106 | + Object propertyValue = accessor.getProperty(propertyHolder[0]); |
| 107 | + |
| 108 | + if (propertyValue != null) { |
| 109 | + persistentEntity.getPropertyPathAccessor(aggregate).setProperty(propertyHolder[0], idValue); |
| 110 | + } else { |
| 111 | + Object idInstance = converter.getEntityInstantiators().getInstantiatorFor(idEntity).createInstance(idEntity, |
| 112 | + new ParameterValueProvider<RelationalPersistentProperty>() { |
| 113 | + @Nullable |
| 114 | + @Override |
| 115 | + public <T> T getParameterValue(Parameter<T, RelationalPersistentProperty> parameter) { |
| 116 | + return (T) idValue; |
| 117 | + } |
| 118 | + }); |
| 119 | + accessor.setProperty(idProperty, idInstance); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private Long querySequence(SqlIdentifier s) { |
| 124 | + |
| 125 | + String sql = dialect.getIdGeneration().createSequenceQuery(s); |
| 126 | + Long sequenceValue = operations.queryForObject(sql, Map.of(), Long.class); |
| 127 | + Assert.state(sequenceValue != null, () -> "No sequence value found for " + s); |
| 128 | + return sequenceValue; |
| 129 | + } |
74 | 130 | }
|
0 commit comments