Spring数据MongoDB示例不起作用

编程入门 行业动态 更新时间:2024-10-11 07:34:12
本文介绍了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: github/mpereira-dev/spring- data-mongo-shared-repo-example

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

要点:

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

  • 您必须使用:
  • @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!

  • 确保您的程序包结构遵循spring约定,以便可以在spring之前发现所有组件,否则,请进行有趣的配置:@ ComponentScan,@ EntityScan和@EnableMongoRepositories.
  • 7.6.2.向所有存储库添加自定义行为

    7.6.2. Adding custom behavior to all repositories

    docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.custom-behaviour-for-all-repositories

    更多推荐

    Spring数据MongoDB示例不起作用

    本文发布于:2023-10-26 07:03:43,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1529418.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:示例   不起作用   数据   Spring   MongoDB

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!