'Spring custom repository for MingoDB
Tell me how to properly implement your repository for mongodb? At the moment, with this configuration, for some reason I get an error .... ((
@Configuration
@EnableMongoRepositories(basePackages = "by.repository.mongo", repositoryBaseClass = BaseMongoRepository.class)
public class MongoConfig {}
@NoRepositoryBean
public interface BaseMongoRepository<T extends BaseDocument> extends MongoRepository<T, String> {
void saveCascade(T document);
}
public interface LimitRepository extends BaseMongoRepository<LimitDocument>, QuerydslPredicateExecutor<LimitDocument> {}
public class LimitRepositoryImpl extends BaseMongoRepositoryImpl<LimitDocument> implements BaseMongoRepository<LimitDocument> {
public LimitRepositoryImpl(MongoEntityInformation<LimitDocument, String> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
}
@Override
public void saveCascade(LimitDocument document) {
save(document);
}
Description:
Parameter 0 of constructor in by.repository.mongo.LimitRepositoryImpl required a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configuration.
UPDATE 1
I have found simple solution... But I would like to know if there are pitfalls in such a decision
public class LimitRepositoryImpl extends BaseMongoRepositoryImpl<LimitDocument> implements BaseMongoRepository<LimitDocument> {
...
public LimitRepositoryImpl(MongoOperations mongoOperations) {
super(getEntityInformationFor(LimitDocument.class, String.class), mongoOperations);
}
...
public static MongoEntityInformation getEntityInformationFor(Class clazz, Class idClazz) {
TypeInformation typeInformation = ClassTypeInformation.from(clazz);
MongoPersistentEntity mongoPersistentEntity = new BasicMongoPersistentEntity(typeInformation);
return new MappingMongoEntityInformation(mongoPersistentEntity, idClazz);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
