首页 > 解决方案 > 开启 data-jpa dep 时注入存储库的问题

问题描述

我注意到,一旦我取消注释 JPA dep,请参见下文:

implementation("org.springframework.boot:spring-boot-starter-jdbc")
implementation ("org.springframework.boot:spring-boot-starter-data-jdbc")
//implementation ("org.springframework.boot:spring-boot-starter-data-jpa")
I do not have any @Entity annotated classes in my code.

我在开始时遇到错误:

The bean 'myRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

实际上,我在启动时为所有存储库都收到了此错误,随机它只是在第一次失败时无法启动并停止。即没有风险,我实际上在存储库中做了一些重复。

我使用: id 'org.springframework.boot' version '2.2.0.RELEASE'版本

我做gradlew clean build了这个项目,以确保我没有任何剩余。

我的存储库类是:

public interface MyRepository extends CrudRepository<MyModel, UUID> {

    @Query(rowMapperClass = MyModelRowMapper.class, value = "select my_uuid, my_code from my_model_table")
    Iterable<MyModel> findMyStuff();
}

我的模型在哪里

public class MyModel {

    @Id
    private UUID id;
    private String code; ...

如果我继续spring-boot-starter-data-jpa发表评论,所有的作品。

想知道,如果有错误或者我仍然错过了设置某些东西

我得到了我的

@Configuration
@EnableJdbcRepositories
public class RepositoryConfig {
}

与所有存储库都位于同一个包中。

毕竟,如果我不包括 jpa,它就可以工作。我的代码中还没有任何 JPA 特定代码。

标签: javaspringspring-data-jpaspring-dataspring-data-jdbc

解决方案


您需要确保不同的存储库仅由正确的 Spring Data Module 处理,方法是将它们和相应的@EnableXXXRepositories注释移动到单独的包中,或者为注释提供适当的过滤器。

否则 Spring Data 将尝试创建错误类型的存储库,然后会失败,例如因为找不到匹配@Id的注解。


推荐阅读