问题
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.
I got an error on start:
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.
I use: id 'org.springframework.boot' version '2.2.0.RELEASE'
version
I did gradlew clean build
of the project to make sure I don't have any leftover.
My repository class is:
public interface MyRepository extends CrudRepository<MyModel, UUID> {
@Query(rowMapperClass = MyModelRowMapper.class, value = "select my_uuid, my_code from my_model_table")
Iterable<MyModel> findMyStuff();
}
where is MyModel
public class MyModel {
@Id
private UUID id;
private String code; ...
All works if I keep spring-boot-starter-data-jpa
commented.
Wonder, if there is a bug or I still missed to setup something.
I got my
@Configuration
@EnableJdbcRepositories
public class RepositoryConfig {
}
sitting in the same package as all repositories live.
And after all, it works if I don't include jpa. I do not have any JPA specific code in my code yet.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/58476286/problems-to-inject-repository-when-data-jpa-dep-is-on