我最近開始研究相關的JBoss EAR,WAR和EJB項目(JBoss 7.7.1)。開發人員正在使用Eclipse進行開發,我們啓動JBoss並通過Eclipse進行調試。如何讓Hibernate,JBoss和Eclipse在單元測試中一起工作?
我可以在Eclipse/JBoss中啓動我們的webapp,沒有任何問題。
鑑於我們根本沒有測試,我決定開始設置測試環境,最初只是爲了測試EJB項目。但是當嘗試啓動測試時,我在配置方面遇到很多困難(我已經查看了所有內容並看到了類似的問題,但對我來說沒有任何效果)。
這個項目的結構是這樣的:
ccc-ejb/
src/
ccc/
<packages and classes under here>
META-INF/persistence.xml
test/
ccc/
<packages and classes under here>
TestDatabase.java
只是爲了完整性(即使該文件仍然沒有被讀)我的persistence.xml文件看起來是這樣的:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="ccc">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasource/ccc-ds</jta-data-source>
<class>ccc.User</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
作爲一個獲得測試環境設置的開始,我有以下測試課程,只是爲了理智檢查環境:
這種public class TestDatabase {
public static void main(String[] args) {
try {
System.out.println(System.getProperty("java.class.path"));
System.out.println(System.class.getResource("META-INF/persistence.xml"));
System.out.println(Class.forName("org.hibernate.ejb.HibernatePersistence"));
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ccc");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.close();
entityManagerFactory.close();
} catch (Throwable t) {
System.out.println(t);
}
}
}
輸出是:
/usr/local/jboss-7.1.1-final/modules/org/hibernate/main/hibernate-entitymanager-4.0.1.Final.jar:/Development/workspace-ccc-dev/ccc-ejb/build/classes:/Development/workspace-ccc-dev/ccc-ejb/lib/json_simple-1.1.jar:/usr/local/jboss-7.1.1-final/modules/javax/activation/api/main/activation-1.1.1.jar:/usr/local/jboss-7.1.1-final/modules/javax/annotation/api/main/jboss-annotations-api_1.1_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/ejb/api/main/jboss-ejb-api_3.1_spec-1.0.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/el/api/main/jboss-el-api_2.2_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/enterprise/api/main/cdi-api-1.0-SP4.jar:/usr/local/jboss-7.1.1-final/modules/javax/enterprise/deploy/api/main/jboss-jad-api_1.2_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/faces/api/main/jboss-jsf-api_2.1_spec-2.0.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/inject/api/main/javax.inject-1.jar:/usr/local/jboss-7.1.1-final/modules/javax/interceptor/api/main/jboss-interceptors-api_1.1_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/jms/api/main/jboss-jms-api_1.1_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/jws/api/main/jsr181-api-1.0-MR1.jar:/usr/local/jboss-7.1.1-final/modules/javax/mail/api/main/mail-1.4.4.jar:/usr/local/jboss-7.1.1-final/modules/javax/management/j2ee/api/main/jboss-j2eemgmt-api_1.1_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/persistence/api/main/hibernate-jpa-2.0-api-1.0.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/resource/api/main/jboss-connector-api_1.6_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/rmi/api/main/jboss-rmi-api_1.0_spec-1.0.4.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/security/auth/message/api/main/jboss-jaspi-api_1.0_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/security/jacc/api/main/jboss-jacc-api_1.4_spec-1.0.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/servlet/api/main/jboss-servlet-api_3.0_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/servlet/jsp/api/main/jboss-jsp-api_2.2_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/servlet/jstl/api/main/jboss-jstl-api_1.2_spec-1.0.2.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/transaction/api/main/jboss-transaction-api_1.1_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/validation/api/main/validation-api-1.0.0.GA.jar:/usr/local/jboss-7.1.1-final/modules/javax/ws/rs/api/main/jboss-jaxrs-api_1.1_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/wsdl4j/api/main/wsdl4j-1.6.2.jar:/usr/local/jboss-7.1.1-final/modules/javax/xml/bind/api/main/jboss-jaxb-api_2.2_spec-1.0.3.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/xml/registry/api/main/jboss-jaxr-api_1.0_spec-1.0.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/xml/rpc/api/main/jboss-jaxrpc-api_1.1_spec-1.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/xml/soap/api/main/jboss-saaj-api_1.3_spec-1.0.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/javax/xml/ws/api/main/jboss-jaxws-api_2.2_spec-2.0.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/hibernate/validator/main/hibernate-validator-4.2.0.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/picketbox/main/picketbox-4.0.7.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/picketbox/main/picketbox-commons-1.0.0.final.jar:/usr/local/jboss-7.1.1-final/modules/org/picketbox/main/picketbox-infinispan-4.0.7.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/as/controller-client/main/jboss-as-controller-client-7.1.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/dmr/main/jboss-dmr-1.1.1.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/logging/main/jboss-logging-3.1.0.GA.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-jaxb-provider/main/resteasy-jaxb-provider-2.3.2.Final-jandex.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-jaxb-provider/main/resteasy-jaxb-provider-2.3.2.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-jaxrs/main/async-http-servlet-3.0-2.3.2.Final-jandex.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-jaxrs/main/async-http-servlet-3.0-2.3.2.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-jaxrs/main/resteasy-jaxrs-2.3.2.Final-jandex.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-jaxrs/main/resteasy-jaxrs-2.3.2.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-multipart-provider/main/resteasy-multipart-provider-2.3.2.Final-jandex.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/resteasy/resteasy-multipart-provider/main/resteasy-multipart-provider-2.3.2.Final.jar:/usr/local/jboss-7.1.1-final/modules/org/jboss/ejb3/main/jboss-ejb3-ext-api-2.0.0.jar
null
class org.hibernate.ejb.HibernatePersistence
java.lang.NoClassDefFoundError: org/hibernate/proxy/EntityNotFoundDelegate
問題:
- 的,因爲,該項目是在Eclipse配置爲EJB項目,並自動有很多JBoss的罐子和JBoss模塊罐子放在構建路徑上(包括Hibernate罐子)。然而,hibernate-entitymanager-4.0.1.Final.jar不會自動添加,我需要自己添加它。如果我不手動添加它,那麼將不會找到org.hibernate.ejb.HibernatePersistence。好奇爲什麼這個jar不會像其他jar文件那樣自動包含?
- persistence.xml資源的輸出爲空?它沒有被發現。我怎樣才能解決這個問題?
當Eclipse構建,它把所有的課在這裏:
/Development/workspace-ccc-dev/ccc-ejb/build/classes
這個目錄肯定是從上面println的輸出類路徑。此外,在類目錄中,特別是我可以看到:
classes/ccc/TestDatabase.class
classes/META-INF/persistence.xml
爲什麼不拾取persistence.xml文件?
- java.lang.NoClassDefFoundError:org/hibernate/proxy/EntityNotFoundDelegate也不好看。當我得到1和2固定時,我會做到這一點,但這裏的任何提示也將非常感激。
謝謝。
啊,是的,謝謝你的擡頭,我完全錯過了。我的意思是「自動」添加到構建路徑中的是,在Eclipse中,需要將JBoss運行時環境與EJB項目相關聯。一旦你這樣做了,一堆瓶子就會自動添加到構建路徑和運行時類路徑中。我說自動的,因爲Eclipse似乎決定添加哪些模塊,罐子等。並非所有運行時所需的罐子都添加了。 Eclipse如何決定添加哪些JBoss jar和模塊似乎是「自動的」或更好的「自動化」。 – lostdorje