Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why could <exclude-unlisted-classes>false</exclude-unlisted-classes> fail to work?

With this 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>

and a simple test:

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));
    }
}

I get

[EL Warning]: 2012-04-17 15:08:18.476--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element

After replacing <exclude-unlisted-classes>false</exclude-unlisted-classes> with a lot of <class> elements, the problem can be fixed, but I'd prefer not to have to remember to edit persistence.xml every time I need to add a new entity or remove an old one. Why doesn't the version with <exclude-unlisted-classes> work?

like image 625
Alexey Romanov Avatar asked Apr 17 '12 11:04

Alexey Romanov


1 Answers

I had faced similar situation

If I generate JPA metamodel, copy paste it in correct pacakge and check it in to svn, and disable metamodel generation, all junit tests were fine

if i generate metamodel with every build, at junit time - embedded glassfish will find all ejb and metamodel fine, but non ejb junit will fail

I had to do this in my 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>
like image 146
Kalpesh Soni Avatar answered Jan 02 '23 09:01

Kalpesh Soni