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

Cannot extend MongoRepository due to private properties / methods / classes #4654

Open
SledgeHammer01 opened this issue Mar 5, 2024 · 5 comments
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged

Comments

@SledgeHammer01
Copy link

Hi,

We implemented some custom functionality by extending JpaRepository, JpaRepositoryFactory, JpaRepositoryFactoryBean and SimpleJpaRepository. It was pretty straight forward once we figured out how to use those 4 classes.

Now we are looking at doing the same for SimpleMongoRepository. However, some crucial properties, methods and classes are package private making it impossible to extend those 4 Mongo variety classes without the use of reflection.

The core problem is really extending MongoRepositoryFactory. For the most basic functionality, we need access to:

  1. MongoOperations -- easy
  2. private <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class domainClass,
    @nullable RepositoryMetadata metadata) -- requires reflection to call
  3. SimpleMongoRepository::setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) -- requires reflection to call
  4. no way to access crudMethodMetadata field without reflection
  5. CrudMethodMetadataPostProcessor also have to use reflection, generic Object here
  6. IndexEnsuringQueryCreationListener class is private, had to c&p it into our code
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 5, 2024
@mp911de
Copy link
Member

mp911de commented Mar 7, 2024

Let's take a step back and discuss what you actually want to achieve. Subclassing SimpleMongoRepository shouldn't require opening up our private methods.

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Mar 7, 2024
@SledgeHammer01
Copy link
Author

Let's take a step back and discuss what you actually want to achieve. Subclassing SimpleMongoRepository shouldn't require opening up our private methods.

Hi Mark,

Same issue we discussed on the JPA Github issue :).

I'm trying to implement custom pagination behavior for Mongo. I'm extending from SimpleMongoRepository, but the main issue stems from trying to get a bean into my impl class.

Similarly to the solution for doing that in the JPA repo impl, I've implemented classes derived from MongoRepositoryFactoryBean and MongoRepositoryFactory.

I don't need to call any of the private methods in my repo impl class, but MongoRepositoryFactory and MongoRepositoryFactoryBean presents issues:

	@Override
	protected Object getTargetRepository(RepositoryInformation information) {

		MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType(),
				information);
		Object targetRepository = getTargetRepositoryViaReflection(information, entityInformation, operations);

		if (targetRepository instanceof SimpleMongoRepository<?, ?> repository) {
			repository.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata());
		}

		return targetRepository;
	}

I basically tried to duplicate that method in my version so I could add my bean in the getTargetRepositoryViaReflection() call.

but the getEntityInformation() method is private and the stuff that method does is private / final.

and the repository.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata()); runs into a bunch of private stuff too.

For the factory bean class:

	@Override
	protected RepositoryFactorySupport createRepositoryFactory() {

		RepositoryFactorySupport factory = getFactoryInstance(operations);

		if (createIndexesForQueryMethods) {
			factory.addQueryCreationListener(
					new IndexEnsuringQueryCreationListener((collectionName, javaType) -> operations.indexOps(javaType)));
		}

		return factory;
	}

all the stuff in the if (this.createIndexesForQueryMethods) block is private.

Although looking at that now, I'm wondering why I didn't override getFactoryInstance() ... that might get rid of the issue for the factory bean class.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 7, 2024
@SledgeHammer01
Copy link
Author

SledgeHammer01 commented Mar 7, 2024

Ok, I tested with overriding MongoRepositoryFactoryBean::getFactoryInstance() instead. Not sure why I didn't see that last time, but that got rid of the need for IndexEnsuringQueryCreationListener and some of the private properties in my factory bean class.

I took another look at MongoRepositoryFactory just to make sure I didn't miss anything. There doesn't seem to be any other way to inject a bean into the repository impl constructor other then adding it to Object targetRepository = getTargetRepositoryViaReflection(information, entityInformation, operations); in MongoRepositoryFactory::getTargetRepository() which depends on some private bits.

@SledgeHammer01
Copy link
Author

SledgeHammer01 commented Mar 7, 2024

Oh yeah, also have to use reflection in a SpringDataMongodbQuery derived class to access the find field. This was for supporting field selection.

@mp911de
Copy link
Member

mp911de commented Mar 8, 2024

By subclassing SimpleMongoRepository you get access to MongoEntityInformation as it is passed to the constructor. If you want to reuse MongoEntityInformation, store it as field in your subclass.

We are not going to open up SpringDataMongodbQuery. Firstly, find is an implementation detail, secondly, you can wrap the fluent API and run mongoOperations.query(domainType).inCollection(collectionName).as(resultType) yourself.

Let me know whether you require anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants