为什么<exclude-unlisted-classes> false </exclude-unlisted-classes>无法正常工作?

时间:2012-04-17 11:11:00

标签: jpa eclipselink

使用此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容器管理的持久性单元期间可能找不到模型类。请使用<class>元素或全局<exclude-unlisted-classes>false</exclude-unlisted-classes>元素验证是否在persistence.xml中引用了您的实体类

使用大量<exclude-unlisted-classes>false</exclude-unlisted-classes>元素替换<class>后,问题可以修复,但我不想每次需要添加时都记得编辑persistence.xml新实体或删除旧实体。为什么<exclude-unlisted-classes>的版本不起作用?

1 个答案:

答案 0 :(得分:1)

我遇到过类似情况

如果我生成JPA元模型,请将其复制粘贴到正确的pacakge并将其检入svn,并禁用元模型生成,所有junit测试都没问题

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

我必须在我的src / 测试 /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>