Skip to content

Conversation

@young0264
Copy link

@young0264 young0264 commented Dec 28, 2025

  • Add validation during RelationalMappingContext.createPersistentEntity construction
  • Add unit tests covering valid and invalid Set-mapped collection scenarios
  • issue link
  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 28, 2025
@young0264 young0264 force-pushed the feat/kevin/validate-set-mapped-collection branch from 6465f79 to 8cfcd67 Compare December 28, 2025 05:54
…g-projectsGH-2061)

- Add validation in RelationalMappingContext.createPersistentEntity
- Add unit tests for validation scenarios

Signed-off-by: euiyoung <euiyoung1403@gmail.com>
Copy link
Contributor

@schauder schauder 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 working on this.

I left various comments with requests for changes.

*
* @param entity the entity to validate
*/
private <T> void validateSetMappedCollectionProperties(RelationalPersistentEntity<T> entity) {
Copy link
Contributor

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

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);
Copy link
Contributor

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);
Copy link
Contributor

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(),
Copy link
Contributor

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()) {
Copy link
Contributor

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

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))
Copy link
Contributor

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();
}

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants