2012-04-17 141 views
7

有了這個persistence.xml爲什麼<exclude-unlisted-classes> false</ exclude-unlisted-classes>失效?

<?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>元素

了很多<class>元素替換<exclude-unlisted-classes>false</exclude-unlisted-classes>後,問題可以是固定的persistence.xml引用,但我不希望每次需要添加新實體或刪除舊實體時,都必須記得編輯persistence.xml。爲什麼<exclude-unlisted-classes>的版本沒有工作?

+6

您的'persistence.xml'和註釋類可以結束在不同的類路徑文件夾中嗎?見http://stackoverflow.com/questions/4885836/no-autodetection-of-jpa-entities-in-maven-verify – axtavt 2012-04-17 11:46:18

+1

是的,它的工作。謝謝! (除了不會擴展'$ {project.build.outputDirectory}',我用'../ classes'取代它;有點討厭,但如果必須的話,我可以忍受它。) – 2012-04-17 12:11:40

+1

@axtavt你可能想要張貼作爲答案,我可以接受它。 – 2012-11-13 16:43:09

回答

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> 
+0

你真的可以通過這樣的類包裝:「 file:../ com/package? – webyildirim 2017-05-05 11:13:12

+0

那不是java如何工作的,你總是用com或org等的dir構建classpath – 2017-05-08 16:06:49