Với điều này persistence.xml
:Tại sao các lớp <exclude-unlisted-class> false</ exclude-unlisted> không hoạt động?
<?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>
và một thử nghiệm đơn giản:
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));
}
}
tôi nhận được
[EL Cảnh báo]: 2012/04/17 15: 08: 18.476-- Bộ sưu tập các loại siêu mô hình trống. Các lớp mô hình có thể không được tìm thấy trong quá trình tìm kiếm thực thể cho Java SE và một số đơn vị lưu trữ được quản lý bởi thùng chứa Java EE. Vui lòng xác nhận rằng các lớp thực thể của bạn được tham chiếu trong persistence.xml sử dụng một trong hai yếu tố
<class>
hoặc một<exclude-unlisted-classes>false</exclude-unlisted-classes>
yếu tố toàn cầu
Sau khi thay thế <exclude-unlisted-classes>false</exclude-unlisted-classes>
với rất nhiều <class>
yếu tố, vấn đề có thể được cố định, nhưng tôi không muốn phải nhớ chỉnh sửa persistence.xml
mỗi lần tôi cần phải thêm một thực thể mới hoặc xóa một thực thể cũ. Tại sao phiên bản không hoạt động với <exclude-unlisted-classes>
?
Các lớp 'persistence.xml' và chú thích của bạn có kết thúc trong các thư mục classpath khác nhau không? Xem http://stackoverflow.com/questions/4885836/no-autodetection-of-jpa-entities-in-maven-verify – axtavt
Có, nó hoạt động. Cảm ơn! (Ngoại trừ nó sẽ không mở rộng '$ {project.build.outputDirectory}' và tôi đã thay thế nó bằng '../ classes'; một chút khó chịu nhưng tôi có thể sống với nó nếu cần.) –
@axtavt Bạn có thể muốn để đăng câu trả lời đó để tôi có thể chấp nhận câu trả lời đó. –