我注意到,一旦我取消注释 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'版本'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();
}
MyModel 在哪里
public class MyModel {
@Id
private UUID id;
private String code; ...
如果我保留 spring-boot-starter-data-jpa
注释,则一切正常。
想知道,是否有错误或者我仍然missed to setup something .
我得到了我的
@Configuration
@EnableJdbcRepositories
public class RepositoryConfig {
}
与所有存储库位于同一个包中。
毕竟,如果我不包含 jpa,它就可以工作。我的代码中还没有任何 JPA 特定代码。
最佳答案
您需要确保不同的存储库仅由正确的 Spring Data 模块处理,方法是将它们和相应的 @EnableXXXRepositories
注释移动到单独的包或为注释提供适当的过滤器。
否则 Spring Data 将尝试创建错误类型的存储库,然后会失败,例如因为找不到匹配的 @Id
注释。
关于java - 当 data-jpa dep 打开时注入(inject)存储库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58476286/