GithubHelp home page GithubHelp logo

Comments (5)

mp911de avatar mp911de commented on June 27, 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.

from spring-data-mongodb.

SledgeHammer01 avatar SledgeHammer01 commented on June 27, 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.

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.

from spring-data-mongodb.

SledgeHammer01 avatar SledgeHammer01 commented on June 27, 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.

from spring-data-mongodb.

SledgeHammer01 avatar SledgeHammer01 commented on June 27, 2024

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

from spring-data-mongodb.

mp911de avatar mp911de commented on June 27, 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.

from spring-data-mongodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.