![](/img/trans.png)
[英]java.lang.IllegalArgumentException: Not a managed type: class models.User
[英]java.lang.IllegalArgumentException: Not a managed type: class
我正在創建具有2個不同數據源的 Spring Boot應用程序。 我已經為單獨的數據庫創建了配置文件。 每個數據庫的實體使用不同的軟件包,而模型使用不同的軟件包。 當我跑步
mvn全新安裝
它會正確創建數據庫以及所有表。 但是在創建存儲庫時總是失敗。 下面,我提供所有必要的詳細信息:
主班
@SpringBootApplication
@EnableAutoConfiguration
public class Demo {
public static void main(String[] args) {
SpringApplication.run(Demo.class, args);
}
}
Application.properties
spring.application.name= nyota-plateform
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.database= MYSQL
spring.jpa.generate-ddl= true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.implicit_naming_strategy= org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.jdbc-url= jdbc:mysql://localhost:3306/DB1?useUnicode=true&createDatabaseIfNotExist=true&useSSL=false
spring.datasource.username= *
spring.datasource.password= *
spring.datasource.dbcp2.max-idle= 10000
authdb.datasource.driver-class-name=com.mysql.jdbc.Driver
authdb.datasource.jdbc-url= jdbc:mysql://localhost:3306/DB2?useUnicode=true&createDatabaseIfNotExist=true&useSSL=false
authdb.datasource.username= *
authdb.datasource.password= *
authdb.datasource.dbcp2.max-idle= 10000
db1配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.test.demo.repo.db1" })
public class DatabaseConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.test.demo.model.db1").persistenceUnit("main").build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
DB2配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "com.test.demo.repo.db2" })
public class DatabaseConfiguration {
@Primary
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "authdb.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "entityManagerFactory2")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSource2") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.test.demo.model.db2").persistenceUnit("main").build();
}
@Bean(name = "transactionManager2")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory2") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
db2模型
@Entity
public class AuthUser {
@Id
@GeneratedValue
private Long id;
@Column
private String name;
@Column(nullable=false,unique=true)
private String username;
@Column(nullable=false)
private String password;
@Column(nullable=false)
private String role;
@Column
private String uniqueId = UUID.randomUUID().toString();
public AuthUser(String name, String username, String password, String role) {
super();
this.name = name;
this.username = username;
this.password = password;
this.role = role;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String email) {
this.username = email;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getUniqueId() {
return uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
}
資料庫
@Repository
public interface AuthUserRepository extends JpaRepository<AuthUser, Long> {
public AuthUser findByUsernameAndPassword(String email,String password);
}
現在錯誤日志:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authUserRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.test.demo.model.auth.AuthUser
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:741) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at com.test.demo.Demo.main(Demo.java:14) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496) [spring-boot-maven-plugin-2.0.2.RELEASE.jar:2.0.2.RELEASE]
有人可以幫忙嗎? 我缺少什么參數或我在這里做錯了什么? 提前致謝!
使用不同的數據源時,我遇到了類似的問題。 使用@Qualifier
批注為我解決了它。 在配置中,可以使用@Qualifier("DB1")
注釋Bean,並使用@Transactional(transactionManager="DB1")
在存儲庫中使用它們。
您能從一個數據源中刪除@primary注釋嗎?
找到我的問題的解決方案,我所要做的就是更新數據庫配置文件中的以下行。
來自:
@EnableJpaRepositories(basePackages = { "com.test.demo.repo.db1" })
至 :
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager", basePackages = { "com.test.demo.repo.db1" })
感謝您的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.