spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

Cannot extend MongoRepository due to private properties / methods / classes

Open SledgeHammer01 opened this issue 1 year ago • 5 comments

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<T> 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

SledgeHammer01 avatar Mar 05 '24 14:03 SledgeHammer01

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 avatar Mar 07 '24 09:03 mp911de

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.

SledgeHammer01 avatar Mar 07 '24 14:03 SledgeHammer01

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 avatar Mar 07 '24 17:03 SledgeHammer01

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

SledgeHammer01 avatar Mar 07 '24 17:03 SledgeHammer01

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.

mp911de avatar Mar 08 '24 09:03 mp911de