Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

mipo256
Copy link
Contributor

@mipo256 mipo256 commented Mar 4, 2025

Closes #2003

Copy link
Member

@mp911de mp911de left a 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.

Object identifier = persistentEntity.getIdentifierAccessor(aggregate).getIdentifier();

if (persistentEntity.getIdProperty().getType().isPrimitive()) {
return identifier instanceof Number num && num.longValue() != 0L;
Copy link
Member

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.

Copy link
Contributor Author

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 ?

Copy link
Member

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.

Copy link
Contributor Author

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.


@BeforeEach
void setUp() {
auditableNamedParameterJdbcTemplate.clearRecordedStatements();
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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.

@mp911de mp911de changed the title GH-2003 ID generation via Sequence should be omitted in UPDATE queries Omit sequence generation for non-new entities and entities with provided identifiers Mar 4, 2025
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 4, 2025
@mp911de mp911de self-assigned this Mar 4, 2025
@mipo256
Copy link
Contributor Author

mipo256 commented Mar 4, 2025

Hey @mp911de! Thanks for review :)

Yeah, I forgot to ask about formatting, my bad, I'll fix it ASAP

@mipo256
Copy link
Contributor Author

mipo256 commented Mar 7, 2025

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

@mp911de
Copy link
Member

mp911de commented Mar 19, 2025

@mipo256 I'd like to come back to this one. I think the only remaining issue is AuditableNamedParameterJdbcTemplate. Please remove that one for the time being in favor of verifying target sequences and entities after the test run for assertions.

@mp911de mp911de added this to the 3.5 RC1 (2025.0.0) milestone Mar 19, 2025
@mipo256
Copy link
Contributor Author

mipo256 commented Mar 19, 2025

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

@mipo256
Copy link
Contributor Author

mipo256 commented Mar 19, 2025

Regarding the article from Vlad you've proposed, Mark @mp911de.

The problem is that this article also utilizes the external library for proxying the DataSource. And Vlad is also using custom listeners, defined in the hypersistence project.

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 AuditableNamedParameterJdbcTemplate and be done with it? I think we eventually need the solution for tracking the queries executed.

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?

@mp911de
Copy link
Member

mp911de commented Mar 19, 2025

The problem is that this article also utilizes the external library for proxying the DataSource. And Vlad is also using custom listeners, defined in the hypersistence project.

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.

@mipo256
Copy link
Contributor Author

mipo256 commented Mar 19, 2025

Done @mp911de, can you re-review please?

Copy link
Member

@mp911de mp911de left a 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

Copy link
Member

@mp911de mp911de left a 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is @TargetSequence?

Copy link
Contributor Author

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
Copy link
Member

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 {
Copy link
Member

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.

Copy link
Contributor Author

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;
Copy link
Member

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.

mp911de pushed a commit that referenced this pull request Apr 9, 2025
…he entity is not new.

Signed-off-by: mipo256 <[email protected]>

Closes #2003
Original pull request: #2005
mp911de added a commit that referenced this pull request Apr 9, 2025
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
mp911de added a commit that referenced this pull request Apr 9, 2025
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
@mp911de
Copy link
Member

mp911de commented Apr 9, 2025

Thank you for your contribution. That's merged and polished now.

@mp911de mp911de closed this Apr 9, 2025
mp911de added a commit that referenced this pull request Apr 9, 2025
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
mp911de added a commit that referenced this pull request Apr 9, 2025
See #2003
Original pull request: #2005
mp911de added a commit that referenced this pull request Apr 9, 2025
See #2003
Original pull request: #2005
mp911de added a commit that referenced this pull request Apr 9, 2025
See #2003
Original pull request: #2005
mp911de added a commit that referenced this pull request Apr 9, 2025
See #2003
Original pull request: #2005
mp911de added a commit that referenced this pull request Apr 9, 2025
See #2003
Original pull request: #2005
mp911de added a commit that referenced this pull request Apr 9, 2025
See #2003
Original pull request: #2005
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Values for @Sequence-annotated property generated on update
3 participants