【问题标题】:Why could <exclude-unlisted-classes>false</exclude-unlisted-classes> fail to work?为什么 <exclude-unlisted-classes>false</exclude-unlisted-classes> 不能工作?
【发布时间】:2012-04-28 17:24:57
【问题描述】:

有了这个persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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="ODP_Server_Test"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <!-- <non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/ODPServerDataSource)</non-jta-data-source> -->
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:unit-testing;create=true" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.target-database" value="DERBY" />
        </properties>
    </persistence-unit>

</persistence>

还有一个简单的测试:

public class RepositoryTest {
    private static Logger logger = LoggerFactory
            .getLogger(RepositoryTest.class);
    private static EntityManagerFactory emf;
    private EntityManager em;
    private RepositoryImpl repo = new RepositoryImpl();

    @BeforeClass
    public static void setUp() {
        try {
            logger.info("Starting in-memory DB for unit tests");
            @SuppressWarnings("unused")
            Class<?> cls = org.apache.derby.jdbc.EmbeddedDriver.class;
            DriverManager.getConnection(
                    "jdbc:derby:memory:unit-testing;create=true").close();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Exception during database startup.");
        }
        try {
            logger.info("Building JPA EntityManager for unit tests");
            emf = Persistence.createEntityManagerFactory("ODP_Server_Test");
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Exception during JPA EntityManager instantiation.");
        }
    }

    @AfterClass
    public static void tearDown() throws SQLException {
        logger.info("Shutting down JPA");
        if (emf != null) {
            emf.close();
        }
        try {
            DriverManager.getConnection(
                    "jdbc:derby:memory:unit-testing;drop=true").close();
        } catch (SQLException ex) {
            if (ex.getSQLState().equals("08006")) {
                logger.info("DB shut down");
            } else {
                throw ex;
            }
        }
        fail("DB didn't shut down");
    }

    @Before
    public void setEM() {
        em = emf.createEntityManager();
        repo.setEntityManager(em);
    }

    @After
    public void flushEM() {
        if (em != null) {
            em.flush();
            em.close();
            em = null;
        }

    }

    @Test
    public void noBlocksInEmptyDB() {
        assertThat(repo.findFunBlock(1), is((FunctionalBlock) null));
    }
}

我明白了

[EL 警告]: 2012-04-17 15:08:18.476--元模型类型的集合为空。在对 Java SE 和某些 Java EE 容器管理的持久性单元进行实体搜索期间,可能未找到模型类。请使用&lt;class&gt; 元素或全局&lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt; 元素验证您的实体类是否在persistence.xml 中引用

用大量&lt;class&gt; 元素替换&lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt; 后,问题可以得到解决,但我不想每次需要添加新实体或删除一个新实体时都必须记住编辑persistence.xml旧的。为什么&lt;exclude-unlisted-classes&gt;的版本不行?

【问题讨论】:

  • 您的persistence.xml 和带注释的类最终会出现在不同的类路径文件夹中吗?见stackoverflow.com/questions/4885836/…
  • 是的,它奏效了。谢谢! (除非它不会扩展 ${project.build.outputDirectory} 并且我将其替换为 ../classes;有点讨厌,但如果必须的话我可以忍受。)
  • @axtavt 您可能想将其发布为答案,以便我接受。
  • 在 Eclipse 中,当您添加了项目 facet 'JPA' 后,您可以右键单击 persistence.xml 文件 => 选择 'JPA Tools' > 'Synhronize class list'。

标签: jpa eclipselink


【解决方案1】:

我也遇到过类似情况

如果我生成 JPA 元模型,将其复制粘贴到正确的 pacakge 中并将其签入到 svn,然后禁用元模型生成,所有 junit 测试都很好

如果我在每次构建时生成元模型,在 junit 时间 - 嵌入式 glassfish 会发现所有 ejb 和元模型都很好,但非 ejb junit 会失败

我必须在我的 src/test/resources/META-INF/persistence.xml 中这样做

<persistence-unit name="test-xxx" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <jar-file>file:../classes</jar-file>
    <shared-cache-mode>ALL</shared-cache-mode>
    <properties>
        <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform"/>
        <property name="eclipselink.logging.timestamp" value="true"/>
        <property name="eclipselink.logging.thread" value="true"/>
        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.logging.parameters" value="true"/>
        <property name="eclipselink.logging.logger" value="JavaLogger"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xxx"/>
        <property name="javax.persistence.jdbc.password" value="xxx"/>
        <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
        <property name="javax.persistence.jdbc.user" value="xxx"/>
    </properties>
</persistence-unit>

【讨论】:

  • 你真的可以像“file:../com/package 那样传递类包吗?
  • 这不是 java 的工作方式,你总是在 com 或 org 等之上使用 dir 构建类路径
  • 这适用于 TomEE 8 Docker:&lt;jar-file&gt;WEB-INF/classes&lt;/jar-file&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 2017-05-12
相关资源
最近更新 更多