Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于IntelliJ中的单独模块导致Hibernate映射错误? 使用具有独立Hibernate实例的多个模块_Hibernate_Maven_Jpa_Persistence Unit - Fatal编程技术网

由于IntelliJ中的单独模块导致Hibernate映射错误? 使用具有独立Hibernate实例的多个模块

由于IntelliJ中的单独模块导致Hibernate映射错误? 使用具有独立Hibernate实例的多个模块,hibernate,maven,jpa,persistence-unit,Hibernate,Maven,Jpa,Persistence Unit,我正在创建一个多模块maven项目,在父级下面有模块ModuleA和模块B Hibernate抛出映射异常 模块使用自己的Hibernate映射和自己的资源文件夹。每个模块独立于其他模块保持其对象。现在,当我尝试在模块a中实例化模块B中的类时,我可以访问所有必需的方法,但当我执行应用程序时,Hibernate会抛出以下错误: Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.example

我正在创建一个多模块maven项目,在父级下面有模块ModuleA和模块B

Hibernate抛出映射异常 模块使用自己的Hibernate映射和自己的资源文件夹。每个模块独立于其他模块保持其对象。现在,当我尝试在模块a中实例化模块B中的类时,我可以访问所有必需的方法,但当我执行应用程序时,Hibernate会抛出以下错误:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.example.ModuleB.Person
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1443)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
我可以将ModuleB中的类的映射另外添加到模块A的Hibernate配置文件中,然后它就可以工作了。但这不是它应该如何工作的,因为每个模块都应该是独立的。理论上,它可以存储在完全不同的RDBMS中

更多背景 最初,我只使用了一个模块,它将其拆分,并希望使其更加倾斜。我干净地执行了mvn并从头编译了所有内容

此外,模块A中的pom.xml依赖于模块B

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>ModuleB</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
HibernateUtilModuleB:

configuration.configure("hibernate.moduleB.cfg.xml");
ModuleA的HibernateUtil类现在大致如下所示:

 /**
 * Hibernate session management
 */
public class HibernateUtilModuleA {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static {
    try {

        Configuration configuration = new Configuration();

        configuration.configure("hibernate.ModuleA.cfg.xml");

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);


    } catch (HibernateException he) {
        System.err.println("Error creating Session: " + he);
        throw new ExceptionInInitializerError(he);
    }
    }


    public static SessionFactory getSessionFactory() {
    return sessionFactory;
    }
}

我也遇到了同样的问题,我用一个复合持久化单元解决了这个问题,但在我的例子中,我使用的是类似eclipselink的Jpa实现。我在这里找到了一些关于Hibernate的信息,我认为解决问题的正确方法是这样

我认为我们需要查看更多的POM文件(对于父级和模块)——尽管我们不需要第三方依赖项,所以您可以截断它们。我们需要知道这些模块是JAR还是打包成独立的WAR,我们需要知道它们之间的关系(依赖关系)。谢谢,这就成功了!你有什么解释为什么Hibernate不能区分不同的配置吗?@MightyApe你能更新你的问题并添加关于你的解决方案的更多细节吗。我包括了关于类文件和配置文件重命名的更多细节。你需要更多的细节吗?少了什么吗?
 /**
 * Hibernate session management
 */
public class HibernateUtilModuleA {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static {
    try {

        Configuration configuration = new Configuration();

        configuration.configure("hibernate.ModuleA.cfg.xml");

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);


    } catch (HibernateException he) {
        System.err.println("Error creating Session: " + he);
        throw new ExceptionInInitializerError(he);
    }
    }


    public static SessionFactory getSessionFactory() {
    return sessionFactory;
    }
}