EJB模块无法访问JPA实体

时间:2015-11-10 22:51:13

标签: maven jpa ejb wildfly ear

我有一个包含4个模块的Maven项目:带有persistence.xml的JPA模块,EJB模块,Webapp模块和EAR模块。

我使用EJB模块来持久化数据库中的实体。现在,当我尝试使用JPA模块中的任何实体时,我在Wildfly 9中收到以下错误:

    22:42:59,298 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.deployment.unit."MusicEJB.jar".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."MusicEJB.jar".POST_MODULE: WFLYSRV0153: Failed to process phase POST_MODULE of deployment "MusicEJB.jar"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:163)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: WFLYSRV0177: Error getting reflective information for class authentication.AuthEJB with ClassLoader ModuleClassLoader for Module "deployment.MusicEJB.jar:main" from Service Module Loader
    at org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex.getClassIndex(DeploymentReflectionIndex.java:70)
    at org.jboss.as.ee.metadata.MethodAnnotationAggregator.runtimeAnnotationInformation(MethodAnnotationAggregator.java:57)
    at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.handleAnnotations(InterceptorAnnotationProcessor.java:107)
    at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.processComponentConfig(InterceptorAnnotationProcessor.java:92)
    at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.deploy(InterceptorAnnotationProcessor.java:77)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:156)
    ... 5 more
    Caused by: java.lang.NoClassDefFoundError: entity/User
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.jboss.as.server.deployment.reflect.ClassReflectionIndex.<init>(ClassReflectionIndex.java:65)
    at org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex.getClassIndex(DeploymentReflectionIndex.java:66)
    ... 10 more
Caused by: java.lang.ClassNotFoundException: entity.User from [Module "deployment.MusicEJB.jar:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130)
    ... 15 more

这是我的persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="MusicProjectPersistence" transaction-type="JTA">    
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/postgresDS</jta-data-source>
        <class>entity.User</class>
        <class>entity.MusicFile</class>
        <class>entity.Playlist</class>

        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/musicdb" />
            <property name="hibernate.connection.username" value="music" />
            <property name="hibernate.connection.password" value="music" />

            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

我的EJB pom.xml中有JPA模块

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您的EJB无法找到您的实体类,因为如果您在构建之后打开了ear文件,您会发现您的JPA模块位于EJB模块旁边而不是lib文件夹中。解决方案非常简单。只需将JPA模块作为依赖项添加到耳模块的POM中。这样,您的JPA模块将成为您耳朵的lib文件夹中的jar,然后您的EJB和Web模块可以在其类路径中找到JPA enitites。添加依赖项后,在ear lib文件夹中构建项目和chek,以确保您的JPA模块是一个jar。

答案 1 :(得分:0)

使用WildFly时,通常没有必要使用EAR打包,而是将所有内容放入WAR中,导致没有不同的类加载器,这可能会为您解决问题。

使用WAR打包查看Adam Bien's example基于maven的项目 - 它不使用JPA模块,但我想你会明白这一点。