-
Notifications
You must be signed in to change notification settings - Fork 379
Add validation warning for invalid Set<T> in @MappedCollection (GH-2061) #2204
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
base: main
Are you sure you want to change the base?
Add validation warning for invalid Set<T> in @MappedCollection (GH-2061) #2204
Conversation
6465f79 to
8cfcd67
Compare
…g-projectsGH-2061) - Add validation in RelationalMappingContext.createPersistentEntity - Add unit tests for validation scenarios Signed-off-by: euiyoung <euiyoung1403@gmail.com>
8cfcd67 to
1dbac6c
Compare
schauder
left a comment
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 working on this.
I left various comments with requests for changes.
| * | ||
| * @param entity the entity to validate | ||
| */ | ||
| private <T> void validateSetMappedCollectionProperties(RelationalPersistentEntity<T> entity) { |
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.
The name is misleading. The validation should not depend on a MappedCollection annotation being present or not.
| this.namingStrategy, this.sqlIdentifierExpressionEvaluator); | ||
| entity.setForceQuote(isForceQuote()); | ||
|
|
||
| // Validate Set<T> properties in @MappedCollection context |
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.
Do not repeat in in-line comments, what is obvious in the code.
| private boolean isSetMappedCollection(RelationalPersistentProperty property) { | ||
| return property.isCollectionLike() | ||
| && Set.class.isAssignableFrom(property.getType()) | ||
| && property.isAnnotationPresent(MappedCollection.class); |
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.
Again, the presence of the annotation is irrelevant.
| elementType.getSimpleName() | ||
| ); | ||
|
|
||
| logger.warn(message); |
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.
We should really throw an exception here. This kind of mapping is just plain invalid and we should fail early.
| "Invalid @MappedCollection usage: Set<%s> in %s.%s. " + | ||
| "Set elements without @Id must not contain entity or collection references. " + | ||
| "Consider using List instead or add @Id to %s.", | ||
| elementType.getSimpleName(), |
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.
Use the full class name, please.
| continue; | ||
| } | ||
|
|
||
| if (prop.isEntity() || prop.isCollectionLike()) { |
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 don't think we need the isCollectionLike condition, since a collection of entities should return isEntity true.
We probably need to check against embedded entities though, since those should not cause a validation failure.
|
|
||
| static class Inherit2 extends Base {} | ||
|
|
||
| // GH-2061 - Tests for Set<T> validation in @MappedCollection context |
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 comment is redundant.
| context.setSimpleTypeHolder(holder); | ||
|
|
||
| // Should not throw exception, just log warning | ||
| assertThatCode(() -> context.getPersistentEntity(AggregateWithInvalidSet.class)) |
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.
These tests, test almost nothing. A proper test should actually test the log message.
Fortunately, testing becomes much easier, when we actually throw exceptions.
| assertThatCode(() -> context.getPersistentEntity(AggregateWithValidSetWithoutReferences.class)) | ||
| .doesNotThrowAnyException(); | ||
| } | ||
|
|
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.
We should have additional tests for:
- element contains a collection that maps to a database array.
- element contains an embedded entity.
Both should not cause a validation exception.
Uh oh!
There was an error while loading. Please reload this page.