【问题标题】:Is it possible to scan Entities in jar files using JPA and hibernate是否可以使用 JPA 和休眠扫描 jar 文件中的实体
【发布时间】:2012-03-22 22:07:35
【问题描述】:

我有以下情况:

  • 项目 A - 包含几个实体并且是独立的
  • 项目 B - 包含几个实体并且是独立的
  • 项目 C - 包含少量实体并依赖于项目 A 和项目 B。

我正在使用 Maven 来管理依赖项和构建。

当我尝试测试项目 A 和项目 B 时,它运行良好。它们每个都有一个 persistence.xml 和一个单独的持久化上下文。

当我运行 Project C 时,它会映射任何实体。我尝试使用自动检测,指定 jar 文件属性...但似乎没有任何效果。

它给了我一个映射异常,说未知实体,不会持久化或读取项目 A 或 B 中的实体。我已在此处发布了 3 个 persistence.xml 文件。

另外,我尝试使用类属性并使用相同的持久上下文,但它就是找不到文件。

非常感谢任何帮助。

提前致谢!

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="A" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
    <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="username"/>
      <property name="hibernate.connection.password" value="password"/>
      <property name="hibernate.connection.url" value="jdbc:oracle:thin:@webdev.epi.web:1521/webdev.world"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
      <property name="hibernate.archive.autodetection" value="class"/>
    </properties>

        </persistence-unit>
</persistence>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="B" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
    <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="username"/>
      <property name="hibernate.connection.password" value="password"/>
      <property name="hibernate.connection.url" value="jdbc:oracle:thin:@webdev.epi.web:1521/webdev.world"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
      <property name="hibernate.archive.autodetection" value="class"/>
    </properties>

        </persistence-unit>
</persistence>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="C" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jar-file>A-0.0.1-SNAPSHOT.jar</jar-file>
                    <jar-file>B-0.0.1-SNAPSHOT.jar</jar-file>
        <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
    <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="username"/>
      <property name="hibernate.connection.password" value="password"/>
      <property name="hibernate.connection.url" value="jdbc:oracle:thin:@webdev.epi.web:1521/webdev.world"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
      <property name="hibernate.archive.autodetection" value="class"/>
    </properties>

        </persistence-unit>
</persistence>

【问题讨论】:

    标签: hibernate jpa hibernate-mapping


    【解决方案1】:

    实际上,我找到了一种使它起作用的方法。 在 Projects A & B 中,我自动检测我的实体,在 Project C 中,我明确列出了映射的类,实体现在被映射并且它可以工作。

    希望它可以帮助别人!

    【讨论】:

      【解决方案2】:

      要在您的项目 C 中也启用自动检测,您可以自己扫描类路径并以编程方式将 JPA 实体注册到 Hibernate。因此无需在persistence.xml 中明确列出这些实体。更多信息请看这里:https://stackoverflow.com/a/41845759/377320

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-06
        • 2016-01-23
        • 2020-10-19
        • 1970-01-01
        • 2010-10-21
        • 2019-05-19
        相关资源
        最近更新 更多