java - 开启 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 特定代码。
解决方案
您需要确保不同的存储库仅由正确的 Spring Data Module 处理,方法是将它们和相应的@EnableXXXRepositories
注释移动到单独的包中,或者为注释提供适当的过滤器。
否则 Spring Data 将尝试创建错误类型的存储库,然后会失败,例如因为找不到匹配@Id
的注解。
推荐阅读
- reactjs - 有没有办法将 svg 添加到我的 img 标签?即使 svg 的路径正确,svg 也不会显示
- python - 如何修复多次播放的pygame音乐?
- python - 如何找到多个csv中每个单元格的平均值
- c - C 二维数组动态分配
- python - GDAL 切割线语法错误,从光栅文件中相对于 shapefile 剪辑多边形
- javascript - 为什么迭代器对象变量未定义?
- javascript - window.open 在函数开始时有效,但在结束时无效
- react-select - React-select:虚拟化选项列表
- html - CSS 网格自动填充和全宽单元格 - 空列
- php - 为什么 Wordpress Redux 框架中的 AddField 方法不起作用?