英文:
Adding custom method to all Spring data Mongo repositories - No property [methodName] found for type
问题
以下是翻译好的内容:
我正在尝试为所有的Mongo仓库添加一个通用的update
方法。
这是我的代码:
BaseMongoRepository.java
@NoRepositoryBean
public interface BaseMongoRepository<T, ID extends Serializable> extends MongoRepository<T, ID> {
void update(T entity, ID id);
}
BaseMongoRepositoryImpl.java
@NoRepositoryBean
public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements BaseMongoRepository<T, ID> {
private final MongoOperations mongoOperations;
public BaseMongoRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoTemplate mongoOperations) {
super(metadata, mongoOperations);
this.mongoOperations = mongoOperations;
}
public void update(T entity, ID id) {
Query query = Query.query(Criteria.where("_id").is(id));
Document doc = new Document();
mongoOperations.getConverter().write(entity, doc);
Update update = Update.fromDocument(new Document("$set", doc));
mongoOperations.updateFirst(query, update, entity.getClass());
}
}
MongoDbConfig.java
@Configuration
@EnableMongoRepositories(repositoryBaseClass = BaseMongoRepositoryImpl.class)
public class MongoDbConfig {
// ...
}
FeedbackMongoRepository
public interface FeedbackMongoRepository extends BaseMongoRepository<FeedbackEntity, String> {
}
但是无论我尝试什么,应用程序启动时总是出现以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feedbackMongoRepository': Invocation of init method failed;
nested exception is org.springframework.data.mapping.PropertyReferenceException:
No property update found for type FeedbackEntity!
非常感谢您的帮助!
英文:
I am trying to add generic method update
to all my Mongo repositories.
Here is my code:
BaseMongoRepository.java
@NoRepositoryBean
public interface BaseMongoRepository<T, ID extends Serializable> extends MongoRepository<T, ID> {
void update(T entity, ID id);
}
BaseMongoRepositoryImpl.java
@NoRepositoryBean
public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements BaseMongoRepository<T, ID> {
private final MongoOperations mongoOperations;
public BaseMongoRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoTemplate mongoOperations) {
super(metadata, mongoOperations);
this.mongoOperations = mongoOperations;
}
public void update(T entity, ID id) {
Query query = Query.query(Criteria.where("_id").is(id));
Document doc = new Document();
mongoOperations.getConverter().write(entity, doc);
Update update = Update.fromDocument(new Document("$set", doc)); // Update.fromDocument(doc);
mongoOperations.updateFirst(query, update, entity.getClass());
}
}
MongoDbConfig.java
@Configuration
@EnableMongoRepositories(repositoryBaseClass = BaseMongoRepositoryImpl.class)
public class MongoDbConfig {
// ...
}
FeedbackMongoRepository
public interface FeedbackMongoRepository extends BaseMongoRepository<FeedbackEntity, String> {
}
But no matter what I try I always end up with this error during application startup:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feedbackMongoRepository': Invocation of init method failed;
nested exception is org.springframework.data.mapping.PropertyReferenceException:
No property update found for type FeedbackEntity!
Any help would be much appreciated!
答案1
得分: 1
问题在于 @EnableMongoRepositories
与存储库位于不同的包中。我通过添加 basePackages
参数来解决了这个问题:
@EnableMongoRepositories(
basePackages = "cz.connect.server",
repositoryBaseClass = BaseMongoRepositoryImpl.class)
注意:这里的代码部分只做了格式化和翻译,没有做任何实质性的修改。
英文:
The problem was that @EnableMongoRepositories
was in different package than repositories. I resolved this by adding basePackages
parameter:
@EnableMongoRepositories(
basePackages = "cz.connect.server",
repositoryBaseClass = BaseMongoRepositoryImpl.class)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论