当前位置: 首页 > 知识库问答 >
问题:

如何在SpringDataJPA和eclipselink中的不同模块上分布实体?

宇文灿
2023-03-14

我目前正在开发一个具有核心模块和不同“扩展”模块的应用程序。核心模块包括基于Spring配置类的JPA配置以及一些“基本”实体及其存储库,这些实体和存储库将在“扩展”模块中使用。扩展模块包含额外的实体类和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.

为了做到这一点,我尝试了三种不同的方法:

  • 创建了一个名为coreEntityManager和setPackagesToScan的LocalContainerEntityManager的LocalContainerEntityManagerFactoryBean,包括核心和扩展包,其中包含指向核心存储库包的@Entity类和@EnableJpaRepositories,扩展的spring配置包含指向核心存储库包的@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;
    }
...

最后我想实现的是一种“乐高”积木,模块及其配置可以简单地作为依赖项添加,相应的配置实体(及其表)可以添加到持久性单元,而无需进一步调整。

有人能帮我吗?

问候

缺陷

共有1个答案

白烨煜
2023-03-14

所以很明显,我看不到树林...我错过了一个扩展实体中的@Entity注释...嗯!面部

感谢大家阅读我的问题并试图帮助我!我真丢脸!

 类似资料:
  • 3. 我不想在我的API中传递所有的参数作为请求体,有些只有查询、变量、输入有些只有查询和变量,就像上面的json数据,我想创建相同的模型,可以在其余的API中使用。目前,我已经为每个API创建了不同的模型。 公共类CreatetRequest{ } 这里我在所有API中复制了我的模型,所以我想创建三个模型类,其中包含所有必需的变量,这些变量在我的引导应用程序中都是常见的,但同时我必须避免在res

  • 我们已经讲到了如何使用模块名称作为调用的一部分,来调用模块中的函数,如示例 7-7 中所示的 nested_modules 函数调用。 文件名: src/main.rs 示例 7-7:通过完全指定模块中的路径来调用函数 如你所见,指定函数的完全限定名称可能会非常冗长。所幸 Rust 有一个关键字使得这些调用显得更简洁。 Rust 的 use 关键字能将你想要调用的函数所在的模块引入到当前作用域中,

  • 我正在使用XML构建调查。调查中的一个(矩阵)问题,具有列和行,如下所示: 我想使用不同的XSLT模板,这取决于 节点。因此,尽管所有问题都是以相同的方式构建的,但它们看起来可能不同。我想用不同的。每个问题类型的XSLT文件,定义问题、列和行的外观。 如果我使用以下代码包括“模块/模板”: 该模板包含问题、行和列的格式定义。如果同一页面上有不同类型的问题,会不会出现干扰,因为定义每种类型的问题、行

  • 我的项目中的三个模型对象(本文末尾的模型和存储库片段)之间确实存在关系。 当我调用时,它会触发三个select查询: (“sql”) (对我来说)那是相当不寻常的行为。在阅读Hibernate文档后,我认为它应该始终使用连接查询。当类中的更改为时,查询没有区别(使用附加选择进行查询),当更改为时,城市类的查询也一样(使用JOIN进行查询)。 当我使用抑制火灾时,有两种选择: 我的目标是在所有情况下

  • 我有一个SBT项目,我发布到Sonatype没有问题,我把它转换成一个多模块SBT项目。现在我想: > 将包含所有聚合子模块的jar/javadoc/sources/pom文件发布到Sonatype(这意味着它们应该使用sbt pgp插件签名) 也将每个单独的子模块发布到Sonatype 我试图为此使用sbt汇编插件,但没有设法走得很远。您是否有一个示例Build.scala文件,它将显示什么是最

  • 我的问题是,如何做到这一点?我如何定义和配置一个生产者和消费者,可以处理不同的avro实体到一个共同的主题? 谢谢你的帮助!