0

我最近开始从事相关的 JBoss EAR、WAR 和 EJB 项目(JBoss 7.7.1)。开发人员使用 Eclipse 进行开发,我们通过 Eclipse 启动 JBoss 和调试。

我可以毫无问题地在 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

问题:

  1. 该项目在 Eclipse 中配置为 EJB 项目,因此自动在构建路径上放置了许多 JBoss jar 和 JBoss 模块 jar(包括 Hibernate jar)。但是hibernate-entitymanager-4.0.1.Final.jar不是自动添加的,需要自己添加。如果我不手动添加它,那么将找不到 org.hibernate.ejb.HibernatePersistence。好奇为什么这个 jar 不像其他 jar 文件那样自动包含?
  2. persistence.xml 资源的输出为空?它没有被发现。我怎样才能解决这个问题?

当 Eclipse 构建时,它把所有的类都放在这里:

/Development/workspace-ccc-dev/ccc-ejb/build/classes

这个目录肯定在上面 println 输出的类路径上。此外,在类目录中,特别是我可以看到:

classes/ccc/TestDatabase.class
classes/META-INF/persistence.xml

为什么没有提取persistence.xml 文件?

  1. java.lang.NoClassDefFoundError: org/hibernate/proxy/EntityNotFoundDelegate 也不好看。当我修复 1 和 2 时,我会解决这个问题,但这里的任何提示也将不胜感激。

谢谢。

4

1 回答 1

0

为什么没有提取persistence.xml 文件?

因为加载它的路径是错误的。它应该是

System.class.getResource("/META-INF/persistence.xml")

如果没有前导斜杠,则路径被解释为相对于 System 类的包。使用前导斜杠,它从类路径的根开始。

至于其他问题,我不知道。但我不明白为什么应该“自动”将任何东西添加到构建路径中。如果您需要构建路径中的 jar,请将其添加到构建路径中。我不明白为什么会有问题。

于 2013-07-20T08:52:52.707 回答