如何将实体分布到 Spring-data-jpa 和 eclipselink 中的不同模块?
How to spread Entities over different modules in Spring-data-jpa and eclipselink?
我目前正在开发一个包含核心模块和不同 "extension" 模块的应用程序。核心模块包括基于 Spring 配置 class 的 JPA 配置以及一些 "basic" 实体及其存储库,它们将在 "extension" 模块中使用。扩展模块包含额外的 Entity classes 和 JPARepositories。为扩展模块启动 JUnit 测试时遇到以下错误:
No [ManagedType] was found for the key class [a.b.c.data.Config] in
the Metamodel - please verify that the [Managed] class was referenced
in persistence.xml using a specific <class>a.b.c.data.Config</class>
property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes>
element.
为此我尝试了三种不同的方法:
- 使用 setPackagesToScan 创建了一个名为 coreEntityManager 的 LocalContainerEntityManagerFactoryBean,包括核心和扩展包,其中包含 @Entity classes 和指向核心存储库包的 @EnableJpaRepositories 和 spring 配置具有指向扩展存储库包的@EnableJpaRepositories 的扩展
- 在 spring 配置中为扩展创建了一个额外的 LocalContainerEntityManagerFactoryBean,它具有自己的 JPaRepository 注释,并且 setPackagesToScan 仅指向包含 @Entity classes 的扩展包。本例中的 coreEntityManager 仅指向核心模块中包含 @Entity classes.
的包
- 使用 setPackagesToScan 创建了一个名为 coreEntityManager 的 LocalContainerEntityManagerFactoryBean,包括核心和扩展包,其中包含 @Entity classes 和指向核心和扩展回购包的 @EnableJpaRepositories
使用前两种方法会出现已经提到的错误,而使用第三种方法则找不到配置存储库:
No qualifying bean of type 'a.b.c.ext.repositories.ConfigRepository' available
方法 1 的代码:
@Configuration
@ComponentScan(basePackages = {"a.b.c.core.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.core.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class CoreJPAConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean coreEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.core");
em.setPackagesToScan("a.b.c.core.data",
"a.b.c.ext.data");
em.setDataSource(dataSource());
return em;
}
...
}
Configuration
@ComponentScan(basePackages = {
"a.b.c.config.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.ext.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class JobSpringConfig {
...
}
方法 2 的代码:
@Configuration
@ComponentScan(basePackages = {"a.b.c.core.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.core.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class CoreJPAConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean coreEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.core");
em.setPackagesToScan("a.b.c.core.data");
em.setDataSource(dataSource());
return em;
}
...
}
@Configuration
@ComponentScan(basePackages = {"a.b.c.config.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.ext.repositories"}, entityManagerFactoryRef = "jobEntityManager")
public class JobSpringConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean jobEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.config");
em.setPackagesToScan("a.b.c.ext.data");
em.setDataSource(dataSource);
return em;
}
方法 3 的代码:
@Configuration
@ComponentScan(basePackages = {"a.b.c.core.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.core.repositories",
"a.b.c.ext.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class CoreJPAConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean coreEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.core");
em.setPackagesToScan("a.b.c.core.data",
"a.b.c.ext.data");
em.setDataSource(dataSource());
return em;
}
...
我最后想要实现的是拥有一种 "Lego" 构建块,其中模块及其配置可以简单地作为依赖项添加,相应的配置实体(及其表)是添加到持久性单元而不需要进一步调整。
有人能帮帮我吗?
亲切的问候
缺陷
所以显然我没能见树不见林...我错过了其中一个扩展实体中的@Entity 注释...DUH! 捂脸
感谢大家阅读我的问题并提供帮助!我真丢人!
我目前正在开发一个包含核心模块和不同 "extension" 模块的应用程序。核心模块包括基于 Spring 配置 class 的 JPA 配置以及一些 "basic" 实体及其存储库,它们将在 "extension" 模块中使用。扩展模块包含额外的 Entity classes 和 JPARepositories。为扩展模块启动 JUnit 测试时遇到以下错误:
No [ManagedType] was found for the key class [a.b.c.data.Config] in
the Metamodel - please verify that the [Managed] class was referenced
in persistence.xml using a specific <class>a.b.c.data.Config</class>
property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes>
element.
为此我尝试了三种不同的方法:
- 使用 setPackagesToScan 创建了一个名为 coreEntityManager 的 LocalContainerEntityManagerFactoryBean,包括核心和扩展包,其中包含 @Entity classes 和指向核心存储库包的 @EnableJpaRepositories 和 spring 配置具有指向扩展存储库包的@EnableJpaRepositories 的扩展
- 在 spring 配置中为扩展创建了一个额外的 LocalContainerEntityManagerFactoryBean,它具有自己的 JPaRepository 注释,并且 setPackagesToScan 仅指向包含 @Entity classes 的扩展包。本例中的 coreEntityManager 仅指向核心模块中包含 @Entity classes. 的包
- 使用 setPackagesToScan 创建了一个名为 coreEntityManager 的 LocalContainerEntityManagerFactoryBean,包括核心和扩展包,其中包含 @Entity classes 和指向核心和扩展回购包的 @EnableJpaRepositories
使用前两种方法会出现已经提到的错误,而使用第三种方法则找不到配置存储库:
No qualifying bean of type 'a.b.c.ext.repositories.ConfigRepository' available
方法 1 的代码:
@Configuration
@ComponentScan(basePackages = {"a.b.c.core.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.core.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class CoreJPAConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean coreEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.core");
em.setPackagesToScan("a.b.c.core.data",
"a.b.c.ext.data");
em.setDataSource(dataSource());
return em;
}
...
}
Configuration
@ComponentScan(basePackages = {
"a.b.c.config.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.ext.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class JobSpringConfig {
...
}
方法 2 的代码:
@Configuration
@ComponentScan(basePackages = {"a.b.c.core.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.core.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class CoreJPAConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean coreEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.core");
em.setPackagesToScan("a.b.c.core.data");
em.setDataSource(dataSource());
return em;
}
...
}
@Configuration
@ComponentScan(basePackages = {"a.b.c.config.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.ext.repositories"}, entityManagerFactoryRef = "jobEntityManager")
public class JobSpringConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean jobEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.config");
em.setPackagesToScan("a.b.c.ext.data");
em.setDataSource(dataSource);
return em;
}
方法 3 的代码:
@Configuration
@ComponentScan(basePackages = {"a.b.c.core.services"})
@EnableJpaRepositories(basePackages = {"a.b.c.core.repositories",
"a.b.c.ext.repositories"}, entityManagerFactoryRef = "coreEntityManager")
public class CoreJPAConfig {
...
@Bean
public LocalContainerEntityManagerFactoryBean coreEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
JpaDialect jpaDialect = new EclipseLinkJpaDialect();
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaDialect(jpaDialect);
em.setJpaPropertyMap(getJpaProperties());
em.setPersistenceUnitName("a.core");
em.setPackagesToScan("a.b.c.core.data",
"a.b.c.ext.data");
em.setDataSource(dataSource());
return em;
}
...
我最后想要实现的是拥有一种 "Lego" 构建块,其中模块及其配置可以简单地作为依赖项添加,相应的配置实体(及其表)是添加到持久性单元而不需要进一步调整。
有人能帮帮我吗?
亲切的问候
缺陷
所以显然我没能见树不见林...我错过了其中一个扩展实体中的@Entity 注释...DUH! 捂脸
感谢大家阅读我的问题并提供帮助!我真丢人!