data-jpa dep 开启时注入 Repository 的问题 [英] Problems to inject Repository when data-jpa dep is on

查看:38
本文介绍了data-jpa dep 开启时注入 Repository 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I noticed, as soon as I un-comment JPA dep, see below:

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.

实际上,我的所有存储库在启动时都收到此错误,随机它首先失败,无法启动和停止.即没有风险我实际上在存储库中做了一些重复.

Actually I got this error for all my repositories on start, randomly it just fails on first it could not start and stops. I.e. there is no risk that I actually do some duplicated in repositories.

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

I use: id 'org.springframework.boot' version '2.2.0.RELEASE' version

我对项目做了 gradlew clean build 以确保我没有任何剩余.

I did gradlew clean build of the project to make sure I don't have any leftover.

我的存储库类是:

public interface MyRepository extends CrudRepository<MyModel, UUID> {

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

MyModel 在哪里

where is MyModel

public class MyModel {

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

如果我对 spring-boot-starter-data-jpa 保持评论,一切正常.

All works if I keep spring-boot-starter-data-jpa commented.

想知道,如果有错误或者我仍然 错过设置.

Wonder, if there is a bug or I still missed to setup something.

我有我的

@Configuration
@EnableJdbcRepositories
public class RepositoryConfig {
}

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

sitting in the same package as all repositories live.

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

And after all, it works if I don't include jpa. I do not have any JPA specific code in my code yet.

推荐答案

您需要确保不同的存储库仅由正确的 Spring Data Module 处理,方法是移动它们和相应的 @EnableXXXRepositories 注释以分离包或通过为注释提供适当的过滤器.

You need to make sure that the different repositories are only handled by the correct Spring Data Module, by moving them and the respective @EnableXXXRepositories annotation to separate packages or by providing appropriate filters to the annotation.

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

Otherwise Spring Data will try to create repositories of the wrong kind which then will fail, for example because no matching @Id annotation is found.

这篇关于data-jpa dep 开启时注入 Repository 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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