当 data-jpa dep 打开时注入存储库时出现问题

问题描述 投票:0回答:2

我注意到,一旦我取消注释 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.

实际上,我在启动时遇到了所有存储库的错误,随机地它首先失败,无法启动并停止。 IE。我实际上不存在在存储库中重复执行一些操作的风险。

我使用:

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 特定代码。

java spring spring-data-jpa spring-data spring-data-jdbc
2个回答
2
投票

您需要确保不同的存储库仅由正确的 Spring Data 模块处理,方法是将它们和相应的

@EnableXXXRepositories
注释移动到单独的包或为注释提供适当的过滤器。

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

@Id
注释。


0
投票

我不确定这是什么原因,但我发现升级到 Spring Boot 3.2.0 时,

JdbcMappingContext
无法确定实体的默认构造函数的参数名称。

Spring 数据将哪个构造函数确定为默认构造函数是随机的,因此我们的某些实体而不是其他实体会收到此错误。

我想出的解决方法是向我们的所有实体添加一个注释为

@PersistenceCreator
的无参数构造函数。

© www.soinside.com 2019 - 2024. All rights reserved.