|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc.core.convert; |
| 17 | + |
| 18 | +import org.apache.commons.logging.Log; |
| 19 | +import org.apache.commons.logging.LogFactory; |
| 20 | + |
| 21 | +import org.springframework.data.mapping.PersistentProperty; |
| 22 | +import org.springframework.data.mapping.PersistentPropertyAccessor; |
| 23 | +import org.springframework.data.relational.core.dialect.Dialect; |
| 24 | +import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
| 25 | +import org.springframework.data.relational.core.sql.SqlIdentifier; |
| 26 | +import org.springframework.data.util.ReflectionUtils; |
| 27 | +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; |
| 28 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 29 | +import org.springframework.lang.Nullable; |
| 30 | +import org.springframework.util.ClassUtils; |
| 31 | +import org.springframework.util.NumberUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Support class for generating identifier values through a database sequence. |
| 35 | + * |
| 36 | + * @author Mikhail Polivakha |
| 37 | + * @author Mark Paluch |
| 38 | + * @since 3.5 |
| 39 | + * @see org.springframework.data.relational.core.mapping.Sequence |
| 40 | + */ |
| 41 | +class SequenceEntityCallbackDelegate { |
| 42 | + |
| 43 | + private static final Log LOG = LogFactory.getLog(SequenceEntityCallbackDelegate.class); |
| 44 | + private final static MapSqlParameterSource EMPTY_PARAMETERS = new MapSqlParameterSource(); |
| 45 | + |
| 46 | + private final Dialect dialect; |
| 47 | + private final NamedParameterJdbcOperations operations; |
| 48 | + |
| 49 | + public SequenceEntityCallbackDelegate(Dialect dialect, NamedParameterJdbcOperations operations) { |
| 50 | + this.dialect = dialect; |
| 51 | + this.operations = operations; |
| 52 | + } |
| 53 | + |
| 54 | + @SuppressWarnings("unchecked") |
| 55 | + protected void generateSequenceValue(RelationalPersistentProperty property, |
| 56 | + PersistentPropertyAccessor<Object> accessor) { |
| 57 | + |
| 58 | + Object sequenceValue = getSequenceValue(property); |
| 59 | + |
| 60 | + if (sequenceValue == null) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + Class<?> targetType = ClassUtils.resolvePrimitiveIfNecessary(property.getType()); |
| 65 | + if (sequenceValue instanceof Number && Number.class.isAssignableFrom(targetType)) { |
| 66 | + sequenceValue = NumberUtils.convertNumberToTargetClass((Number) sequenceValue, |
| 67 | + (Class<? extends Number>) targetType); |
| 68 | + } |
| 69 | + |
| 70 | + accessor.setProperty(property, sequenceValue); |
| 71 | + } |
| 72 | + |
| 73 | + protected boolean hasValue(PersistentProperty<?> property, PersistentPropertyAccessor<Object> propertyAccessor) { |
| 74 | + |
| 75 | + Object identifier = propertyAccessor.getProperty(property); |
| 76 | + |
| 77 | + if (property.getType().isPrimitive()) { |
| 78 | + |
| 79 | + Object primitiveDefault = ReflectionUtils.getPrimitiveDefault(property.getType()); |
| 80 | + return !primitiveDefault.equals(identifier); |
| 81 | + } |
| 82 | + |
| 83 | + return identifier != null; |
| 84 | + } |
| 85 | + |
| 86 | + private @Nullable Object getSequenceValue(RelationalPersistentProperty property) { |
| 87 | + |
| 88 | + SqlIdentifier sequence = property.getSequence(); |
| 89 | + |
| 90 | + if (sequence != null && !dialect.getIdGeneration().sequencesSupported()) { |
| 91 | + LOG.warn(""" |
| 92 | + Aggregate type '%s' is marked for sequence usage but configured dialect '%s' |
| 93 | + does not support sequences. Falling back to identity columns. |
| 94 | + """.formatted(property.getOwner().getType(), ClassUtils.getQualifiedName(dialect.getClass()))); |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + String sql = dialect.getIdGeneration().createSequenceQuery(sequence); |
| 99 | + return operations.queryForObject(sql, EMPTY_PARAMETERS, (rs, rowNum) -> rs.getObject(1)); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments