为所有Spring Data Mongo仓库添加自定义方法 – 未找到类型的属性[methodName]。

huangapple go评论69阅读模式
英文:

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&lt;T, ID extends Serializable&gt; extends MongoRepository&lt;T, ID&gt; {

    void update(T entity, ID id);
}

BaseMongoRepositoryImpl.java

@NoRepositoryBean
public class BaseMongoRepositoryImpl&lt;T, ID extends Serializable&gt; extends SimpleMongoRepository&lt;T, ID&gt; implements BaseMongoRepository&lt;T, ID&gt; {

    private final MongoOperations mongoOperations;

    public BaseMongoRepositoryImpl(MongoEntityInformation&lt;T, ID&gt; metadata, MongoTemplate mongoOperations) {

        super(metadata, mongoOperations);
        this.mongoOperations = mongoOperations;
    }

    public void update(T entity, ID id) {

        Query query = Query.query(Criteria.where(&quot;_id&quot;).is(id));

        Document doc = new Document();
        mongoOperations.getConverter().write(entity, doc);
        Update update = Update.fromDocument(new Document(&quot;$set&quot;, 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&lt;FeedbackEntity, String&gt; {
}

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 &#39;feedbackMongoRepository&#39;: 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 = &quot;cz.connect.server&quot;,
    repositoryBaseClass = BaseMongoRepositoryImpl.class)

huangapple
  • 本文由 发表于 2020年10月5日 08:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64201182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定