0

我正在使用 Hibernate 4.0.1.Final。我正在使用 Assemby 插件将我的所有依赖项包含在我的 JAR 项目中。但是,Hibernate 无法找到我的一门课程,而且拼写正确。在我的 hibernate.cfg.xml 文件(位于我的 JAR 的根目录)中,我有

<mapping class="com.myco.fdr.myproject.model.UserRole" />  

我像这样加载文件

final Configuration configuration = new AnnotationConfiguration();  
configuration.configure("hibernate.cfg.xml");

但是当执行上面的代码时,我得到了这个异常......</p>

javax.ejb.EJBTransactionRolledbackException: Unable to load class declared as <mapping class="com.mainco.fdr.myco.model.UserRole"/> in the configuration:
    at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)
    at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
    at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:249)
    at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:268)
    at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:138)
    at $Proxy84.execute(Unknown Source)
    at org.jboss.resource.adapter.quartz.inflow.QuartzJob.execute(QuartzJob.java:57)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
Caused by: org.hibernate.MappingException: Unable to load class declared as <mapping class="com.mainco.fdr.myco.model.UserRole"/> in the configuration:
    at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:545)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
    at org.cbco.subco.dido.service.AbstractServiceProvider.getSessionFactory(AbstractServiceProvider.java:66)
    at org.cbco.subco.dido.service.AbstractServiceProvider.initServices(AbstractServiceProvider.java:118)
    at org.cbco.subco.dido.quartz.ProcessOrdersJob.execute(ProcessOrdersJob.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
    at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
    at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: No ClassLoaders found for: com.mainco.fdr.myco.model.UserRole
    at org.jboss.mx.loading.LoadMgr3.beginLoadTask(LoadMgr3.java:212)
    at org.jboss.mx.loading.RepositoryClassLoader.loadClassImpl(RepositoryClassLoader.java:521)
    at org.jboss.mx.loading.RepositoryClassLoader.loadClass(RepositoryClassLoader.java:415)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:542)
    ... 31 more

有问题的类出现在我的 JAR 文件的根目录中(也许它应该出现在“类”目录中?)。无论如何,这就是我如何使用 Assembler 插件...</p>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

如何解决这个 ClassNotFoundException?

4

1 回答 1

0

你有persistence.xml吗?
添加到您的persistence.xml 财产

 <property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml"/>  
于 2012-08-06T18:05:23.823 回答