2012-04-17 8 views
7

Z tego persistence.xml:Dlaczego <wykluczenia-klasy niepubliczne> false</ exclude-unlisted-classes> nie działają?

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

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

uzyskać

[EL Ostrzeżenie]: 2012-04-17 15: 08: 18.476-- Zbiór typów metamodelu jest pusty. Klasy modelowe mogły nie zostać znalezione podczas wyszukiwania jednostek dla Java SE i niektórych jednostek trwałości zarządzanych kontenerami Java EE. Proszę sprawdzić, czy podmiot klas są odniesione w persistence.xml stosując albo <class> elementów lub globalną <exclude-unlisted-classes>false</exclude-unlisted-classes> elementu

Po wymianie <exclude-unlisted-classes>false</exclude-unlisted-classes> z wieloma <class> elementów, problem może być stałe, ale wolałbym, aby nie pamiętaj, aby edytować persistence.xml za każdym razem, gdy muszę dodać nowy obiekt lub usunąć stary. Dlaczego nie działa wersja z <exclude-unlisted-classes>?

+6

Czy Twoje klasy 'persistence.xml' i annotated mogą znajdować się w różnych folderach ścieżki klas? Zobacz http://stackoverflow.com/questions/4885836/no-autodetection-of-jpa-entities-in-maven-verify – axtavt

+1

Tak, zadziałało. Dzięki! (Z wyjątkiem tego, że nie rozszerzyłoby to '$ {project.build.outputDirectory}' i zastąpiłem go '../ classes', trochę nieprzyjemnym ale mogę z tym żyć, jeśli muszę.) –

+1

@axtavt Możesz chcieć zamieścić to jako odpowiedź, abym mógł to zaakceptować. –

Odpowiedz

1

miałem do czynienia podobną sytuację

Gdybym generować WZP metamodel, skopiować wkleić go w prawidłowym pacakge i sprawdzić go do svn i wyłączanie generacja metamodel, wszystkie testy JUnit były w porządku

gdybym generować metamodel z każdej budowy, w czasie junit - wbudowany GlassFish znajdzie wszystkie ejb i metamodel w porządku, ale nie EJB JUnit zawiedzie

musiałem zrobić to w moim src/testu /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

Czy naprawdę możesz przekazać pakiety klasy takie jak ten " plik: ../ com/package? – webyildirim

+0

to nie działa jak java, zawsze budujesz ścieżkę klas z katalogiem na com lub org itp. –

Powiązane problemy