我正在一个 Web 项目中研究 MAVEN 和 JPA。不知何故,我在 NoClassDefFoundError 上遇到了一个奇怪的错误,如下所示:
Caused by: java.lang.NoClassDefFoundError: WEB-INF/classes/org/huahsin/WebEngineering/Role (wrong name: org/huahsin/WebEngineering/Role)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at com.ibm.ws.classloading.internal.AppClassLoader.internalFindClass(AppClassLoader.java:214)
at [internal classes]
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:171)
at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.ejb.packaging.NativeScanner.getClassesInJar(NativeScanner.java:136)
at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:487)
at org.hibernate.ejb.Ejb3Configuration.scanForClasses(Ejb3Configuration.java:861)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:606)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:75)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:287)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1419)
... 27 more
我把我的persistence.xml
放在下面src/main/resources/META-INF
,内容如下:
<persistence-unit name="WebEngineering" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.huahsin.WebEngineering.Role</class>
<class>org.huahsin.WebEngineering.User</class>
...
...
</persistence-unit>
在我的 POM.xml 中,我正在使用这些库:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.3.Final</version>
</dependency>
当我检查 WAR 文件时,我Role.class
看到了在WEB-INF/classes/org/huahsin/WebEngineering
文件夹中。我不确定我是否将持久性文件放置在正确的文件夹中,或者实际上我在 MAVEN 中使用了错误的 JPA 库?
问题已经解决了
这个问题是由于persistence.xml
没有放在正确的地方造成的。它假设放在src/
目录中,而不是我错误地放在resources/
目录下。我最初的想法是,因为我使用的是 MAVEN,所以persistence.xml
假设在resources/
目录内,但实际上我错了。