-
Notifications
You must be signed in to change notification settings - Fork 359
R2DBC @Sequence annotation support #2028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...main/java/org/springframework/data/r2dbc/core/mapping/IdGeneratingBeforeSaveCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2020-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.r2dbc.core.mapping; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.reactivestreams.Publisher; | ||
import org.springframework.data.r2dbc.dialect.R2dbcDialect; | ||
import org.springframework.data.r2dbc.mapping.OutboundRow; | ||
import org.springframework.data.r2dbc.mapping.event.BeforeSaveCallback; | ||
import org.springframework.data.relational.core.mapping.RelationalMappingContext; | ||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; | ||
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; | ||
import org.springframework.data.relational.core.sql.SqlIdentifier; | ||
import org.springframework.r2dbc.core.DatabaseClient; | ||
import org.springframework.r2dbc.core.Parameter; | ||
import org.springframework.util.Assert; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* R2DBC Callback for generating ID via the database sequence. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public class IdGeneratingBeforeSaveCallback implements BeforeSaveCallback<Object> { | ||
|
||
private static final Log LOG = LogFactory.getLog(IdGeneratingBeforeSaveCallback.class); | ||
|
||
private final RelationalMappingContext relationalMappingContext; | ||
private final R2dbcDialect dialect; | ||
|
||
private final DatabaseClient databaseClient; | ||
|
||
public IdGeneratingBeforeSaveCallback(RelationalMappingContext relationalMappingContext, R2dbcDialect dialect, | ||
DatabaseClient databaseClient) { | ||
this.relationalMappingContext = relationalMappingContext; | ||
this.dialect = dialect; | ||
this.databaseClient = databaseClient; | ||
} | ||
|
||
@Override | ||
public Publisher<Object> onBeforeSave(Object entity, OutboundRow row, SqlIdentifier table) { | ||
Assert.notNull(entity, "The aggregate cannot be null at this point"); | ||
|
||
RelationalPersistentEntity<?> persistentEntity = relationalMappingContext.getPersistentEntity(entity.getClass()); | ||
|
||
if (!persistentEntity.hasIdProperty() || // | ||
!persistentEntity.getIdProperty().hasSequence() || // | ||
!persistentEntity.isNew(entity) // | ||
) { | ||
return Mono.just(entity); | ||
} | ||
|
||
RelationalPersistentProperty property = persistentEntity.getIdProperty(); | ||
SqlIdentifier idSequence = property.getSequence(); | ||
|
||
if (dialect.getIdGeneration().sequencesSupported()) { | ||
return fetchIdFromSeq(entity, row, persistentEntity, idSequence); | ||
} else { | ||
illegalSequenceUsageWarning(entity); | ||
} | ||
|
||
return Mono.just(entity); | ||
} | ||
|
||
private Mono<Object> fetchIdFromSeq(Object entity, OutboundRow row, RelationalPersistentEntity<?> persistentEntity, | ||
SqlIdentifier idSequence) { | ||
String sequenceQuery = dialect.getIdGeneration().createSequenceQuery(idSequence); | ||
|
||
return databaseClient // | ||
.sql(sequenceQuery) // | ||
.map((r, rowMetadata) -> r.get(0)) // | ||
.one() // | ||
.map(fetchedId -> { // | ||
row.put( // | ||
persistentEntity.getIdColumn().toSql(dialect.getIdentifierProcessing()), // | ||
Parameter.from(fetchedId) // | ||
); | ||
return entity; | ||
}); | ||
} | ||
|
||
private static void illegalSequenceUsageWarning(Object entity) { | ||
LOG.warn(""" | ||
It seems you're trying to insert an aggregate of type '%s' annotated with @Sequence, but the problem is RDBMS you're | ||
working with does not support sequences as such. Falling back to identity columns | ||
""".stripIndent().formatted(entity.getClass().getName())); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
.../java/org/springframework/data/r2dbc/core/mapping/IdGeneratingBeforeSaveCallbackTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2020-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.r2dbc.core.mapping; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.reactivestreams.Publisher; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.r2dbc.dialect.MySqlDialect; | ||
import org.springframework.data.r2dbc.dialect.PostgresDialect; | ||
import org.springframework.data.r2dbc.mapping.OutboundRow; | ||
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext; | ||
import org.springframework.data.relational.core.mapping.Sequence; | ||
import org.springframework.data.relational.core.sql.SqlIdentifier; | ||
import org.springframework.r2dbc.core.DatabaseClient; | ||
import org.springframework.r2dbc.core.Parameter; | ||
|
||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
/** | ||
* Unit tests for {@link IdGeneratingBeforeSaveCallback}. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
class IdGeneratingBeforeSaveCallbackTest { | ||
|
||
@Test | ||
void testIdGenerationIsNotSupported() { | ||
R2dbcMappingContext r2dbcMappingContext = new R2dbcMappingContext(); | ||
r2dbcMappingContext.getPersistentEntity(SimpleEntity.class); | ||
MySqlDialect dialect = MySqlDialect.INSTANCE; | ||
DatabaseClient databaseClient = mock(DatabaseClient.class); | ||
|
||
IdGeneratingBeforeSaveCallback callback = new IdGeneratingBeforeSaveCallback(r2dbcMappingContext, dialect, | ||
databaseClient); | ||
|
||
OutboundRow row = new OutboundRow("name", Parameter.from("my_name")); | ||
SimpleEntity entity = new SimpleEntity(); | ||
Publisher<Object> publisher = callback.onBeforeSave(entity, row, SqlIdentifier.unquoted("simple_entity")); | ||
|
||
StepVerifier.create(publisher).expectNext(entity).expectComplete().verify(); | ||
assertThat(row).hasSize(1); // id is not added | ||
} | ||
|
||
@Test | ||
void testEntityIsNotAnnotatedWithSequence() { | ||
R2dbcMappingContext r2dbcMappingContext = new R2dbcMappingContext(); | ||
r2dbcMappingContext.getPersistentEntity(SimpleEntity.class); | ||
PostgresDialect dialect = PostgresDialect.INSTANCE; | ||
DatabaseClient databaseClient = mock(DatabaseClient.class); | ||
|
||
IdGeneratingBeforeSaveCallback callback = new IdGeneratingBeforeSaveCallback(r2dbcMappingContext, dialect, | ||
databaseClient); | ||
|
||
OutboundRow row = new OutboundRow("name", Parameter.from("my_name")); | ||
SimpleEntity entity = new SimpleEntity(); | ||
Publisher<Object> publisher = callback.onBeforeSave(entity, row, SqlIdentifier.unquoted("simple_entity")); | ||
|
||
StepVerifier.create(publisher).expectNext(entity).expectComplete().verify(); | ||
assertThat(row).hasSize(1); // id is not added | ||
} | ||
|
||
@Test | ||
void testIdGeneratedFromSequenceHappyPath() { | ||
R2dbcMappingContext r2dbcMappingContext = new R2dbcMappingContext(); | ||
r2dbcMappingContext.getPersistentEntity(WithSequence.class); | ||
PostgresDialect dialect = PostgresDialect.INSTANCE; | ||
DatabaseClient databaseClient = mock(DatabaseClient.class, RETURNS_DEEP_STUBS); | ||
long generatedId = 1L; | ||
|
||
when(databaseClient.sql(Mockito.anyString()).map(Mockito.any(BiFunction.class)).one()).thenReturn( | ||
Mono.just(generatedId)); | ||
|
||
IdGeneratingBeforeSaveCallback callback = new IdGeneratingBeforeSaveCallback(r2dbcMappingContext, dialect, | ||
databaseClient); | ||
|
||
OutboundRow row = new OutboundRow("name", Parameter.from("my_name")); | ||
WithSequence entity = new WithSequence(); | ||
Publisher<Object> publisher = callback.onBeforeSave(entity, row, SqlIdentifier.unquoted("simple_entity")); | ||
|
||
StepVerifier.create(publisher).expectNext(entity).expectComplete().verify(); | ||
assertThat(row).hasSize(2) | ||
.containsEntry(SqlIdentifier.unquoted("id"), Parameter.from(generatedId)); | ||
} | ||
|
||
static class SimpleEntity { | ||
|
||
@Id | ||
private Long id; | ||
|
||
private String name; | ||
} | ||
|
||
static class WithSequence { | ||
|
||
@Id | ||
@Sequence(sequence = "seq_name") | ||
private Long id; | ||
|
||
private String name; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.