Spring数据MongoDB示例不起作用 [英] Spring data MongoDB example not working

查看:189
本文介绍了Spring数据MongoDB示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想从

I am just trying to get the "Adding custom behaviour to all repositories" example from the reference doc working. But for the following class:

public class MyRepositoryImpl<T, ID extends Serializable>
  extends SimpleJpaRepository<T, ID> implements MyRepository<T, ID> {

  public void sharedCustomMethod(ID id) {
    // implementation goes here
  }
}

我收到编译错误

找不到适合SimpleJpaRepository()的构造函数
构造函数org.springframework.data.jpa.repository.support.SimpleJpaRepository.SimpleJpaRepository(java.lang.Class,javax.persistence.EntityManager)不适用
(实际和正式参数列表的长度不同)
构造函数org.springframework.data.jpa.repository.support.SimpleJpaRepository.SimpleJpaRepository(org.springframework.data.jpa.repository.support.JpaEntityInformation,javax.persistence.EntityManager)不适用
(实际和正式论点列表的长度不同)

no suitable constructor found for SimpleJpaRepository()
constructor org.springframework.data.jpa.repository.support.SimpleJpaRepository.SimpleJpaRepository(java.lang.Class,javax.persistence.EntityManager) is not applicable
(actual and formal argument lists differ in length)
constructor org.springframework.data.jpa.repository.support.SimpleJpaRepository.SimpleJpaRepository(org.springframework.data.jpa.repository.support.JpaEntityInformation,javax.persistence.EntityManager) is not applicable
(actual and formal argument lists differ in length)

我该如何工作?

推荐答案

我并不咸,但是不幸的是,实现起来并不十分清楚.

I'm not salty but implementing this is unfortunately not clear at all.

查看图像以获取完整的示例github: https://github.com/mpereira-dev/spring- data-mongo-shared-repo-example

Look at the image for a complete example, github: https://github.com/mpereira-dev/spring-data-mongo-shared-repo-example

要点:

  1. 您必须在基本接口上使用 @NoRepositoryBean ,以便spring不会为此创建一个bean.
  2. 基本接口的名称并不重要,就像将自定义方法添加到单个存储库中一样.
  3. 实现类的名称也没有关系
  4. 在实现类中添加 @Repository 会产生以下错误:
  1. You must use @NoRepositoryBean on the base interface so spring does not create a bean for it.
  2. The name of the base interface doesn't matter like it does for adding custom methods to a single repo.
  3. The name of the implementing class also doesn't matter
  4. Adding @Repository to the implementing class will yield the following error :

构造函数的参数0 com.example.demo.repository.ImplementationRepoNameDoesntMatterEitherUnlikeAddingCustomMethodsToSingleRepo 需要一个类型的bean 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' 找不到.

Parameter 0 of constructor in com.example.demo.repository.ImplementationRepoNameDoesntMatterEitherUnlikeAddingCustomMethodsToSingleRepo required a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' that could not be found.

  1. 您必须使用:

@EnableMongoRepositories( repositoryBaseClass = ImplementationRepoNameDoesntMatterEitherUnlikeAddingCustomMethodsToSingleRepo.class)

@EnableMongoRepositories( repositoryBaseClass = ImplementationRepoNameDoesntMatterEitherUnlikeAddingCustomMethodsToSingleRepo.class)

如果不告诉spring有关您的基类的信息,则会出现此错误(spring尝试将方法解析为查询):

to tell spring about your base class if you don't you will get this error (spring tries to parse the method into a query):

起因: org.springframework.data.mapping.PropertyReferenceException:否 找到类型为Person的属性someMethod!

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property someMethod found for type Person!

  1. 确保您的程序包结构遵循spring约定,以便可以在spring之前发现所有组件,否则,请进行有趣的配置:@ ComponentScan,@ EntityScan和@EnableMongoRepositories.

7.6.2.向所有存储库添加自定义行为

7.6.2. Adding custom behavior to all repositories

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆