-
Notifications
You must be signed in to change notification settings - Fork 355
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
Omit sequence generation for non-new entities and entities with provided identifiers #2005
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for having a look. Formatting is terribly off. Please make sure to use the formatting config (and import style) at https://github.com/spring-projects/spring-data-build/tree/main/etc/ide.
I left quite a few comments given that this is quite a sized PR touching up on many aspects. Let me know if you require more detail and whether my feedback is too short at places.
...src/main/java/org/springframework/data/jdbc/core/mapping/IdGeneratingBeforeSaveCallback.java
Outdated
Show resolved
Hide resolved
Object identifier = persistentEntity.getIdentifierAccessor(aggregate).getIdentifier(); | ||
|
||
if (persistentEntity.getIdProperty().getType().isPrimitive()) { | ||
return identifier instanceof Number num && num.longValue() != 0L; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice to myself: We should introduce an utility to determine whether a primitive is set to its initial value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can introduce it in separate commit in the same PR, it seems that it is natural to do it in the context of the current ticket. What do you think @mp911de ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering an utility in Spring Data Commons. Let me verify whether this is a good idea or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll wait for your response. I think I can help with this as well.
...src/main/java/org/springframework/data/jdbc/core/mapping/IdGeneratingBeforeSaveCallback.java
Outdated
Show resolved
Hide resolved
...ava/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java
Outdated
Show resolved
Hide resolved
...ava/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java
Outdated
Show resolved
Hide resolved
...ava/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java
Outdated
Show resolved
Hide resolved
...ava/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java
Outdated
Show resolved
Hide resolved
...ava/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java
Outdated
Show resolved
Hide resolved
...ava/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java
Outdated
Show resolved
Hide resolved
|
||
@BeforeEach | ||
void setUp() { | ||
auditableNamedParameterJdbcTemplate.clearRecordedStatements(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When already having an integration test, it doesn't make much sense to verify against statements. Rather, we should the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, but it does not seem to be clear for me how we can verify the SELECT
statements executed to query the sequence. We can create a wrapper around ID generating callback and try to utilize it. I am not sure which approach is better to be honest @mp911de
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a blog post by Vlad on verifying and counting statements see https://vladmihalcea.com/how-to-detect-the-n-plus-one-query-problem-during-testing/
I think that one can serve as input for decorating our data sources to be tested.
Hey @mp911de! Thanks for review :) Yeah, I forgot to ask about formatting, my bad, I'll fix it ASAP |
Hey @mp911de! I've fixed almost all comments left, but several comments I've left open for discussion since it is not clear for me what needs to be done |
@mipo256 I'd like to come back to this one. I think the only remaining issue is |
Hey @mp911de! I understand that it is coming into the RC, I'll resolve it very shortly. Thank you! I'll tag you once the remaining stuff is fixed |
Regarding the article from Vlad you've proposed, Mark @mp911de. The problem is that this article also utilizes the external library for proxying the Let me just clarify - for now, do we want to actually bring any of those libraries into the project, or do we want to just get rid of P.S: That comments thread remained unanswered. I can create an issue, it seems that it fits more in commons module, what do you think? |
DataSource Proxy wouldn't be an issue, however, custom listeners are. So for the time being, let's verify target sequences and entities after the test run for assertions. Re Util for primitive values: I'll handle that during the merge. |
…n UPDATE queries Signed-off-by: mipo256 <[email protected]>
Done @mp911de, can you re-review please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is much cleaner now, thank you. I'm going to merge this PR in the next few days so we can ship it with 3.5 RC1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some last remarks for your future pull request. I took care of these during polishing, highly recommended that you check out both polishing commits as I moved the sequence into RelationalPersistentProperty
.
} else { | ||
if (idSequence.isPresent()) { | ||
LOG.warn(""" | ||
It seems you're trying to insert an aggregate of type '%s' annotated with @TargetSequence, but the problem is RDBMS you're |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is @TargetSequence
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was an old name of the @Sequence
annotation, sorry, I've missed it out 🙈
@@ -16,59 +17,73 @@ | |||
import org.springframework.util.Assert; | |||
|
|||
/** | |||
* Callback for generating ID via the database sequence. By default, it is registered as a | |||
* bean in {@link AbstractJdbcConfiguration} | |||
* Callback for generating ID via the database sequence. By default, it is registered as a bean in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we do want to avoid dependency inversions, this class should ideally now know anything about AbstractJdbcConfiguration
.
propertyAccessor.setProperty(persistentEntity.getRequiredIdProperty(), idValue); | ||
}); | ||
} | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other files using space formatting , such as IdGeneratingBeforeSaveCallbackTest
. As regular contributor I bet you can do better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I've imported the spring data code formatter into Intelij. I'll double-check my formatting, thanks
|
||
@Test // DATAJDBC-98 | ||
@Autowired | ||
SimpleSeqRepository simpleSeqRepository; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After revising these tests, we could have gone for testing through JdbcAggregateOperations
, we don't really need repositories for identifier testing. But it is now what it is.
…he entity is not new. Signed-off-by: mipo256 <[email protected]> Closes #2003 Original pull request: #2005
Refine assignment flow and use early returns where possible. Cache empty MapSqlParameterSource. Reduce dependency on RelationalMappingContext using a lower-level abstraction signature. Simplify names. Use default value check from Commons. Fix log warning message. Add missing since tags. Remove superfluous annotations and redundant code. Tweak documentation wording. Closes #2003 Original pull request: #2005
Sequence details are now maintained on the property level instead of using the entity level. This is a more accurate representation of the underlying model and that properties are annotated and not entities. It also allows future extension of expanding sequence support to general properties. Extract abstract support class for sequence generation. Move types to org.springframework.data.jdbc.core.convert to resolve package cycles. See #2003 Original pull request: #2005
Thank you for your contribution. That's merged and polished now. |
Sequence details are now maintained on the property level instead of using the entity level. This is a more accurate representation of the underlying model and that properties are annotated and not entities. It also allows future extension of expanding sequence support to general properties. Extract delegate for sequence generation. Move types to org.springframework.data.jdbc.core.convert to resolve package cycles. See #2003 Original pull request: #2005
Closes #2003