Skip to content

Inconsistency when having multiple MappingContexts in an application vs a single one #2613

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
joao-borges opened this issue Apr 22, 2022 · 3 comments
Assignees

Comments

@joao-borges
Copy link

Hello,

I am upgrading my company applications to spring boot 2.6.6, which includes spring-data-commons 2.6.3.
Doing that, I bumped into this problem: spring-projects/spring-data-rest#2095.

This problem was supposed to be fixed in 2.6.1 version but I am still experiencing it, and after a long debugging journey I might have found the problem.

The inconsistency I found lives in here:

	public PersistentEntity<?, ? extends PersistentProperty<?>> getRequiredPersistentEntity(Class<?> type) {

		Assert.notNull(type, "Domain type must not be null!");

		if (contexts.size() == 1) {
			return contexts.iterator().next().getRequiredPersistentEntity(type);
		}

		return getPersistentEntity(type).orElseThrow(() -> {
			return new MappingException(String.format(
					"Cannot get or create PersistentEntity for type %s! PersistentEntities knows about %s MappingContext instances and therefore cannot identify a single responsible one. Please configure the initialEntitySet through an entity scan using the base package in your configuration to pre initialize contexts.",
					type.getName(), contexts.size()));
		});
	}

When there's a single mapping context, an optimization is done to invoke getRequiredPersistentEntity on that single one.
That works well with the solution made in the other issue reported, a proxy class for an entity is correctly related to its concrete class and everything works fine.

However, when we have multiple contexts, we dive into this method:

	public Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> getPersistentEntity(Class<?> type) {

		for (MappingContext<?, ? extends PersistentProperty<?>> context : contexts) {
			if (context.hasPersistentEntityFor(type)) {
				return Optional.of(context.getRequiredPersistentEntity(type));
			}
		}

		return Optional.empty();
	}

In here, before invoking getRequiredPersistentEntity, it's being called hasPersistentEntityFor with the class being a HibernateProxy.
I am not sure why - but hasPersistentEntityFor is returning false in that situation, preventing the subsequent method to be called.
The other contexts I have are unrelated so they are expected to return false.

Looking at the caller method, this causes an exception that prevents the completion of the request.
But when a single context is involved, no issue happens.

I am attaching 2 simple spring-boot applications that reproduce the inconsistency. One with a single context, one with more than one.
single context
multiple contexts
For both, the problem is reproduced by using the following cURL command:

curl --location --request GET 'localhost:8088/api/test/userGroups?projection=projection'

Using the single-context app, no error will happen. In the multi-context, the error will happen.

I appreciate the time and if you guys are able to look into this please.
Our upgrade to latest spring boot is now blocked.

Thanks

Joao
[email protected]

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 22, 2022
@odrotbohm
Copy link
Member

This suspiciously sounds like a duplicate of #2589, resolved in Spring Data Commons 2.6.4. Would you mind giving a recent version a try?

@odrotbohm odrotbohm added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels May 4, 2022
@odrotbohm odrotbohm self-assigned this May 4, 2022
@joao-borges
Copy link
Author

@odrotbohm it works. thanks for looking at it

@odrotbohm
Copy link
Member

Happy coding!

@odrotbohm odrotbohm removed the status: waiting-for-feedback We need additional information before we can continue label May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants