Skip to content

Create PersistentEntity only for user class in case of proxy instance. #2486

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
wants to merge 2 commits into from

Conversation

christophstrobl
Copy link
Member

This PR makes sure to only create a PersistentEntity for an actual user type. Therefore ClassTypeInformation now considers the given type and not only the user one for both equals and hashcode. This makes sure we can distinguish TypeInformation for a proxy from the one of a user class.
The AbstractMapping context will take care of unpacking proxied types and registering the created entity for both the user as well as the proxy type information.
Prior to this commit build time generated proxy instances would have been able to pollute the context depending on the order they had been served by the initial entity set.

Closes: #2485

This commit makes sure to only create a PersistentEntity for an actual user type. Therefore ClassTypeInformation now considers the given type and not only the user one for both equals and hashcode. This makes sure we can distinguish TypeInformation for a proxy from the one of a user class.
The AbstractMapping context will take care of unpacking proxied types and registering the created entity for both the user as well as the proxy type information.
Prior to this commit build time generated proxy instances would have been able to pollute the context depending on the order they had been served by the initial entity set.
@sdeleuze
Copy link
Contributor

Since the related issue breaks MongoDB support in Spring Native 0.11.0-SNAPSHOT, could it be possible to merge this PR in the Spring Data Commons release that will be included in Spring Boot 2.6.0?

I can validate it works with snapshots once merged.

@odrotbohm
Copy link
Member

@sdeleuze – Sure thing!

Copy link
Member

@odrotbohm odrotbohm left a comment

Choose a reason for hiding this comment

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

Does it make sense to avoid TypeInformation.isProxyTypeInformation() and change the client code to simply compare the original TypeInformation instance with the one returned from ….getUserTypeInformation()? That'd avoid leaking the proxy case into the API and keep the abstraction to "there's a case in which the original user type is different from what we deal with" without revealing the cause.

@mp911de
Copy link
Member

mp911de commented Oct 26, 2021

That was exactly my thought too, to not expose isProxyTypeInformation. I would even suggest not exposing getUserTypeInformation at all and address the issue locally in AbstractMappingContext until we recognize a need to expose usertypeinformation.

@odrotbohm
Copy link
Member

I think that wouldn't work currently, as CTI partially works with the unwrapped type already. IIRC, we decided to remove all unwrapping from the TypeInformation abstractions so that they'd always see the real type, no matter what. I guess the changes in equals(…)/hashCode() try to achieve the same. I'd actually like to stick to the former so that we avoid the problem in the first place.

I guess it might still be helpful to expose getUserTypeInformation() as we likely have code flying around that uses ClassTypeInformation directly and most of that will want to work on the user type in the first place.

@mp911de mp911de added this to the 2.6 GA (2021.1.0) milestone Oct 27, 2021
@mp911de mp911de marked this pull request as ready for review October 27, 2021 07:20
@mp911de mp911de added the type: enhancement A general enhancement label Oct 27, 2021
mp911de pushed a commit that referenced this pull request Oct 27, 2021
This commit makes sure to only create a PersistentEntity for an actual user type. Therefore ClassTypeInformation now considers the given type and not only the user one for both equals and hashcode. This makes sure we can distinguish TypeInformation for a proxy from the one of a user class.
The AbstractMapping context will take care of unpacking proxied types and registering the created entity for both the user as well as the proxy type information.
Prior to this commit build time generated proxy instances would have been able to pollute the context depending on the order they had been served by the initial entity set.

Closes #2485
Original pull request: #2486.
mp911de added a commit that referenced this pull request Oct 27, 2021
Remove isProxyTypeInformation from TypeInformation. Use computeIfAbsent to fall back to user type information when attempting to add a entity to the MappingContext.

See #2485
Original pull request: #2486.
@mp911de
Copy link
Member

mp911de commented Oct 27, 2021

That's merged now.

@mp911de mp911de closed this Oct 27, 2021
@mp911de mp911de deleted the issue/2485 branch October 27, 2021 07:21
@mp911de mp911de self-assigned this Oct 27, 2021
odrotbohm added a commit that referenced this pull request Oct 27, 2021
A few further tweaks: ClassTypeInformation now never unwraps the user type to consistently work with the type originally presented in the lookup. That allows us to get rid of the custom equals(…)/hashCode() methods and removes the need to deal with original and user type in the same CTI instance.

The augmented caching of PersistentEntities in AbstractMappingContext now primarily happens in doAddPersistentEntity(…). We still need to implement a cache manipulation for the case that a user type has caused the creation of a PersistentEntity first and we see a lookup for the proxy type later. Before actually processing the TypeInformation we now try to lookup an already existing PersistentEntity for the user type and augment the cache if one was found.

See #2485
Original pull request: #2486.
@sdeleuze
Copy link
Contributor

I have been able to run our data-mongodb Spring Native sample 10 times without errors using snapshots so this fix seems to be effective, thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PersistentEntities should only be created for user class in case of proxy instance.
4 participants