Hibernate:从不同类路径的jar中自动加载标记为@Entity的类

时间:2018-05-23 14:45:36

标签: java hibernate

我有应用程序(.war),它使用来自不同类路径的其他jar文件中的实体。我的persistance.xml看起来像

<?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="EAP" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/pscDataSource2</jta-data-source>

        <class>com.myPackage.entity.MyEntity</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.jdbc.batch_size" value="50" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.generate_statistics" value="false" />
            <property name="hibernate.id.new_generator_mappings" value="false" />
            <property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
        </properties>
    </persistence-unit>

</persistence>

使用此xml,我成功加载了 MyEntity 类。但是我想避免在persistance.xml文件中列出所有必需的类。我发现的是<property name="hibernate.archive.autodetection" value="class, hbm" />但是使用这种方法我需要列出我想要加载实体的jar文件。所以我的persistance.xml文件现在看起来如下,并且工作正常

<?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="EAP" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/pscDataSource2</jta-data-source>

        <jar-file>lib/myJar.jar</jar-file> //new line

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.jdbc.batch_size" value="50" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.generate_statistics" value="false" />
            <property name="hibernate.id.new_generator_mappings" value="false" />
            <property name="hibernate.ejb.interceptor" value="com.myPackage.interceptor.DelegatingInterceptor" />
            <property name="hibernate.archive.autodetection" value="class, hbm" /> //new line
        </properties>
    </persistence-unit>

</persistence>

问题:如果我从我的应用程序依赖它们,是否可以从jar中加载实体而不列出它们?我不使用Spring

1 个答案:

答案 0 :(得分:0)

根据Red Hat支持,空<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <button class="outlet">test</button> </body>属性是解决方案。这个对我有用。所以现在我的persistence.xml看起来像

<jar-file></jar-file>