【问题标题】:Error when trying to add hibernate to spring project [closed]尝试将休眠添加到spring项目时出错[关闭]
【发布时间】:2012-08-29 08:01:41
【问题描述】:

我正在尝试将 hibernate 添加到我的 spring 项目中以实现数据库持久性,我已经下载了 hibernate 4.1.6 最新的稳定版本,并且我正在使用 spring 3.1。

但是,当我尝试运行我的项目时,我得到:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 
'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': 
Invocation of init method failed; nested exception is java.lang.ClassFormatError:   
Absent Code attribute in method that is not native or abstract in class file  
javax/validation/Validation

到目前为止,我添加到项目中的外部 jar 是:

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.6.Final.jar
hibernte-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
hibernate-entitymanager-4.1.6.Final.jar
slf4j-api-1.6.1.jar

我的 pom.xml 包含以下依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate.version}</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <scope>runtime</scope>
    <version>1.1.2</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>


<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
</dependency>
</dependencies>

感谢您的帮助,如果对我的问题有任何更改,请告诉我。

丹尼尔

【问题讨论】:

标签: spring hibernate maven spring-mvc pom.xml


【解决方案1】:

Maven 存储库中的 javaee-api 依赖项似乎存在问题,作为解决方案,您可以切换到替代项

   <dependency>
       <groupId>org.apache.geronimo.specs</groupId>
       <artifactId>geronimo-ejb_3.1_spec</artifactId>
       <version>1.0</version>
       <scope>provided</scope>
   </dependency>
   <dependency>
       <groupId>org.apache.geronimo.specs</groupId>
       <artifactId>geronimo-jpa_2.0_spec</artifactId>
       <version>1.0</version>
       <scope>provided</scope>
   </dependency>

致谢:here 讨论了此解决方案。

另见Failed to load ApplicationContext with @ContextConfiguration(classes={ ... })JUnit testcase passes with eclipse but fails with maven build

更新如果你的 pom.xml 中有 Hibernate 实体管理器和 Java EE 依赖项,为什么你下载了单独的 hibernate jar 文件(包括 JPA 2.0 实现),这是不明白的

【讨论】:

  • 哦,它可能归结为遵循各种设置教程,稍后我会对其进行精简以删除重复项,尽管您的解决方案有效,但谢谢您!
猜你喜欢
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
相关资源
最近更新 更多