【问题标题】:Autodetection of JPA entities from JBoss module从 JBoss 模块自动检测 JPA 实体
【发布时间】:2016-01-29 12:12:41
【问题描述】:

我有一个 EJB 模块 M,包含在一个 EAR 模块中。

M 依赖于一个 jar 文件 data-model.jar,作为 JBoss 模块 安装并使用 jboss-deployment-structure.xml 文件。

该模块可以正常工作,但我无法帮助将每个实体添加为 persistence.xml 中的<class> 标记。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
         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_2_0.xsd">

<persistence-unit name="persistenceUnitName" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/datasource</jta-data-source>

    <class>com.bla.persistence.entity.EntityOne</class>
    <class>com.bla.persistence.entity.EntityTwo</class>
    ...

所以问题是:有没有办法指定与从 JBoss 模块加载它们的持久性单元关联的持久性托管类,而无需将它们全部作为类标签添加到 persistence.xml 中?

默认情况下,持久性单元与 包含在持久性单元根目录中的带注释的托管持久性类相关联e.d.persistence.xml 所在的 jar 文件) (docs)。

我已尝试添加:

<exclude-unlisted-classes>false</exclude-unlisted-classes>

但它不会改变任何东西(我已经读过 Java EE 应用程序使用这个值作为默认值)。

我也可以使用:

 <jar-file>?</jar-file>

但我不知道如何引用 JBoss 模块(并且硬编码模块文件的名称对我来说似乎是一种非常糟糕的做法)。

合作:

  • JBoss EAP 6.4
  • 休眠 4.2.18
  • JPA 2

【问题讨论】:

    标签: hibernate jpa jboss jboss-eap-6 persistence.xml


    【解决方案1】:

    您不需要在persistence.xml 中将实体添加为&lt;class&gt; 标签

    只需在persistence.xml中添加自动扫描参数

    <!-- Scan for annotated classes and Hibernate mapping XML files -->
    <property name="hibernate.archive.autodetection" value="class, hbm" />
    

    为了使persistence.xml和实体对象工作在同一个类路径中。

    如果您在与实体相同的类路径中没有 persistence.xml,请在 persistence.xml 中添加此配置

    <persistence ...>
    <persistence-unit ...>
        <jar-file>${project.build.outputDirectory}</jar-file>
        ...
    </persistence-unit>
    

    【讨论】:

    • 我这样做了,但仍然找不到实体。有什么想法吗?
    猜你喜欢
    • 2013-04-11
    • 2011-06-20
    • 2011-11-12
    • 2017-05-06
    • 2017-02-02
    • 2016-02-11
    • 2010-09-22
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多