BeanDefinitionOverrideException,Spring Data JPA和JDBC bean冲突

huangapple go评论77阅读模式
英文:

BeanDefinitionOverrideException, Spring Data JPA and JDBC beans conflict

问题

我计划在一个应用程序中同时使用Spring Data JPA和JDBC存储库(一些实体使用JPA,其他使用JDBC),使用Spring Boot 2.1.7

public interface UserRepository2 extends CrudRepository<User, Integer> {

}

@Entity
@Table(name = "userstab")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;

	private String name;

	private String email;

	...
}

运行应用程序时出现错误:

无法在 null 中注册名为 'userRepository2' 的 bean。已经在 null 中定义了具有该名称的 bean,并且禁用了覆盖。
操作:
考虑为其中一个 bean 重新命名,或通过设置 **spring.main.allow-bean-definition-overriding=true** 来启用覆盖。

在 null 中定义的名为 'userRepository2' 的无效 bean 定义:无法为 bean 'userRepository2' 注册 bean 定义
[Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]];作用域=;抽象=false;lazyInit=false;autowireMode=0;dependencyCheck=0;autowireCandidate=true;primary=false;factoryBeanName=null;factoryMethodName=null;initMethodName=null;destroyMethodName=null] 
已经存在一个 [Root bean: class [org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean]];

我尝试过不使用Spring Data JDBC的Spring Boot测试应用程序 => 没有问题

我尝试过在该应用程序中使用spring.main.allow-bean-definition-overriding=true => 没有问题

因此,Spring Data会从一个存储库接口创建两个存储库bean(JPA和JDBC),并且这两个bean的名称都是接口的名称(userRepository2)。
我无法为这两个存储库bean设置不同的名称,也不能使用spring.main.allow-bean-definition-overriding=true。

对于为每个存储库/实体选择JPA/JDBC,有哪些最佳实践?

附注:我找到了带有basePackages属性的@EnableJdbcRepositories,但我不确定这是否是一个好主意。

英文:

I planned to use Spring Data JPA and JDBC repositories in one application (some entities JPA and other JDBC) with Spring Boot 2.1.7.

public interface UserRepository2 extends CrudRepository&lt;User, Integer&gt; {

}

@Entity
@Table(name = &quot;userstab&quot;)
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;

	private String name;

	private String email;

	...
}

Running application I got the error:

The bean &#39;userRepository2&#39;, defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

Action:
Consider renaming one of the beans or enabling overriding by setting **spring.main.allow-bean-definition-overriding=true**

Invalid bean definition with name &#39;userRepository2&#39; defined in null: Cannot register bean definition [Root bean: class [org.springframework.data.**jpa**.repository.support.**JpaRepositoryFactoryBean**]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean &#39;userRepository2&#39;: There is already [Root bean: class [org.springframework.data.**jdbc**.repository.support.**JdbcRepositoryFactoryBean**]; 

I tried Spring Boot test-application without Spring Data JDBC => no problems

I tried this application with spring.main.allow-bean-definition-overriding=true => no problems

So Spring Data creates 2 repository beans (JPA and JDBC) from one repository interface with the name of interface (userRepository2).
I can't set different names for this 2 repo beans, can't use spring.main.allow-bean-definition-overriding=true.

What are the best practices for choosing JPA/JDBC for every repository/entity?

PS: found @EnableJdbcRepositories with basePackages property, but i am not sure that it is a good idea

答案1

得分: 1

这几乎可以确定是由于旧版Spring Data JDBC中的一个已修复错误引起的。请确保您至少拥有以下版本之一的spring-data-jdbc:1.0.12、1.1.1、2.0.0。有关详细信息,请参见DATAJDBC-437

英文:

This is almost certainly due to a fixed bug in older versions of Spring Data JDBC. Make sure that you have at least one of the following versions of spring-data-jdbc

1.0.12
1.1.1
2.0.0

See DATAJDBC-437 for details.

huangapple
  • 本文由 发表于 2020年8月26日 19:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63596829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定