Comments (5)
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.
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.
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.
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.
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)
- Error Query JPA in INNER...INNER Id HOT 1
- MappingContext should not consider collection like types being an entity. HOT 1
- NPE when use MongoTemplate#updateFirst, MongoTemplate#updateMulti with Query#withHint HOT 2
- auto-index-creation will block app to start when one Mongo cluster node is offline HOT 6
- Reading entity with null value for primitive field fails since #4464 HOT 1
- Centering the documentation HOT 1
- Release 4.1.11 (2023.0.11)
- Release 4.2.5 (2023.1.5)
- Release 4.3 RC1 (2024.0.0)
- Upgrade to MongoDB 5.0 driver
- Aggregation `allowDiskUse` should not be `false` by default
- Incorrect Lock used in DefaultMessageListenerContainer HOT 1
- Redesign allowDiskUse attribute of Meta annotation.
- Nested JSON Handling Issue in Generic Class HOT 2
- Problem with replacement of @DBRef to @DocumentReference HOT 2
- AggregationResults include documents outside the MatchOperation when there is a MergeOperation present HOT 3
- Can't use properties in `@Indexed#expireAfter` HOT 1
- Align OffsetScrolling to zero-based indexes
- Criteria.regex is converted to String when applying to ID fields HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-mongodb.