java - Hibernate 抽象方法错误

标签 java hibernate spring-mvc gradle entitymanager

我在 gradle 项目上使用最新版本的 spring、hibernate 和 spring data jpa,但出现 AbstractMethodError 异常。这是堆栈跟踪的一部分

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EntityManagerFactory' defined in class path resource [com/ashken/config/JPADataConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.AbstractMethodError
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[na:na]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[na:na]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[na:na]
    ... 93 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManagerFactory]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.AbstractMethodError
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[na:na]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[na:na]
    ... 102 common frames omitted
Caused by: java.lang.AbstractMethodError: null
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:278) ~[na:na]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[na:na]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802) ~[na:na]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[na:na]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) ~[na:na]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:319) ~[na:na]
    at com.ashken.config.JPADataConfig.entityManagerFactory(JPADataConfig.java:56) ~[na:na]
    at com.ashken.config.JPADataConfig$$EnhancerBySpringCGLIB$$a3ded6c2.CGLIB$entityManagerFactory$1(<generated>) ~[na:na]
    at com.ashken.config.JPADataConfig$$EnhancerBySpringCGLIB$$a3ded6c2$$FastClassBySpringCGLIB$$91e60d8f.invoke(<generated>) ~[na:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[na:na]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355) ~[na:na]
    at com.ashken.config.JPADataConfig$$EnhancerBySpringCGLIB$$a3ded6c2.entityManagerFactory(<generated>) ~[na:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[na:na]

这是gradle文件

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.akhikhl.gretty'
apply plugin: "war"
war {
    baseName = 'Test'
    version = '1.0'
}

//sourceCompatibility = 1.7

repositories {
    mavenLocal()
    mavenCentral()
     maven { url "http://repository.pentaho.org/artifactory/repo/" }
    maven { url "https://repo.spring.io/libs-release" }
}

dependencies {
  // Dependencies goes here

  compile 'javax.servlet:javax.servlet-api:3.1.0'
  compile 'org.springframework:spring-webmvc:4.2.5.RELEASE'
  compile 'org.springframework:spring-context-support:4.2.5.RELEASE'
  compile 'org.springframework:spring-orm:4.2.5.RELEASE'
  compile 'org.springframework:spring-jdbc:4.2.5.RELEASE'
  compile 'org.springframework:spring-tx:4.2.5.RELEASE'
  compile 'org.springframework:spring-aop:4.2.5.RELEASE'

  compile 'org.springframework.security:spring-security-core:4.0.4.RELEASE'
  compile 'org.springframework.security:spring-security-web:4.0.4.RELEASE'
  compile 'org.springframework.security:spring-security-config:4.0.4.RELEASE'
  compile 'org.springframework.security.oauth:spring-security-oauth2:2.0.8.RELEASE'

  compile 'org.springframework.data:spring-data-jpa:1.9.4.RELEASE'
  compile 'org.springframework.data:spring-data-mongodb:1.8.2.RELEASE'
  compile 'org.springframework.data:spring-data-envers:0.3.0.RELEASE'

  compile 'javax.servlet:jstl:1.2'
  compile 'org.hibernate:hibernate-core:5.1.0.Final'
  compile 'org.hibernate:hibernate-entitymanager:5.1.0.Final'
  compile 'org.hibernate:hibernate-validator:5.2.4.Final'

  compile 'org.apache.commons:commons-dbcp2:2.1.1'

  compile 'mysql:mysql-connector-java:5.1.38'
  compile 'org.postgresql:postgresql:9.4.1207.jre7'


  compile 'com.fasterxml.jackson.core:jackson-core:2.7.3'
  compile 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
  compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'

  compile 'org.apache.poi:poi:3.13'
  compile 'net.sf.jasperreports:jasperreports:6.2.0'

  compile 'org.apache.logging.log4j:log4j-core:2.5'
  compile 'org.apache.logging.log4j:log4j-api:2.5'
  compile 'commons-fileupload:commons-fileupload:1.3.1'
  compile 'commons-io:commons-io:2.4'
  compile 'commons-logging:commons-logging:1.2'

  compile 'com.itextpdf:itextpdf:5.5.8'
  compile 'com.lowagie:itext:2.1.7'
  compile 'org.olap4j:olap4j:1.1.0'

  compile 'org.thymeleaf:thymeleaf-spring4:3.0.0.BETA01'
}

//Gretty Embedded Jetty
buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath 'org.akhikhl.gretty:gretty:+'
  }
}
gretty {
  port = 8080
  contextPath = '/'
  servletContainer = 'jetty9' //tomcat7 or tomcat8
}

eclipse {

  wtp {
    component {

      //define context path, default to project folder name
      contextPath = '/'

    }

  }
}

和我的 jpa 配置类,它永远不会给我旧版本的工件带来问题

package com.ashken.config;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;



@Configuration
@EnableJpaRepositories(entityManagerFactoryRef="EntityManagerFactory",transactionManagerRef="TransactionManager", basePackages="com.ashken.repositories")
@EnableTransactionManagement
public class JPADataConfig {

    @Autowired 
    Environment environment;

    @Bean(name = "DataSource")
    public DataSource getAuthDataSource()
    {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/ngo");
        ds.setUsername("root");
        ds.setPassword("");
        return ds;
    }

    @Autowired
    @Bean(name="EntityManagerFactory")
    EntityManagerFactory entityManagerFactory(@Qualifier("DataSource") DataSource dataSource) {

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("com.ashken.entities");

        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        jpaProperties.setProperty("hibernate.hbm2ddl.auto", "update");
        jpaProperties.setProperty("hibernate.connection.autocommit", "false");
        //jpaProperties.setProperty("hibernate.show_sql", "true");
        entityManagerFactoryBean.setJpaProperties(jpaProperties);
        entityManagerFactoryBean.afterPropertiesSet();

        return entityManagerFactoryBean.getObject();
    }


     @Autowired
     @Bean(name = "TransactionManager")
        JpaTransactionManager transactionManager(@Qualifier("EntityManagerFactory") EntityManagerFactory entityManagerFactory) {
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(entityManagerFactory);
            return transactionManager;
        }

}

最佳答案

您可能知道,AbstractMethodError 有时是由 JAR incompability 引起的。我尝试了自己的 gradle 构建,并通过查看您的 WAR 文件发现了可能导致此问题的原因:

  1. 您使用旧版本的 hibernate-envers。您没有要求这样做,但您声明了对 spring-data-envers 的依赖(因此 spring-data-envers 需要 hibernate-envers JAR),这可能会导致问题。

  2. 我发现您有 bcmail-jdk14-138.jar、bcprov-jdk14-138.jar 和 stax-api-1.0.x.jar 的重复项。删除其中之一也许可以解决您的问题。

编辑: 其中一些 jar 来自名为传递依赖的东西,这在 maven 中非常常见和自然。和 gradle 。当不同的库/框架对另一个具有不同版本的库具有相同的依赖关系时,就会出现冲突。

例如,hibernate 和 spring 对 cglib 具有相同的依赖关系。但是hibernate声明它们依赖于cglib-1.2,而spring决定依赖于cglib-1.0。最后,当您构建 WAR(或带有其依赖项的 JAR)时,您的构建将同时带来 cglib-1.0 和 cglib-1.2。

how do i get rid of them

您可以尝试执行:

gradle dependencies

或与任何 provided variants ,然后通过在 build.gradle 中声明依赖项来排除重复的 jar,如下所示:

compile ('com.lowagie:itext:2.1.7') {
  exclude group: 'org.bouncycastle', module: 'bcmail-jdk14'
}

如果您发现不习惯使用命令行比较重复的依赖项,您可以尝试:

  1. 添加apply plugin: "maven"到你的build.gradle,然后执行gradle install命令。

  2. <project-root-dir>/build ,将 pom-default.xml 复制到 <project-root-dir>/pom.xml .

  3. 使用支持 m2eclipse 的 eclipse 打开此项目(我认为所有最新的 eclipse 版本都应该支持 maven)。

  4. 打开 pom.xml 并将选项卡更改为“依赖关系层次结构”,然后您可以在右侧面板中“排除”依赖关系。在 pom.xml ,排除的依赖项将用

    标记

    <exclusion> .... </exclusion>

  5. 转换已在 pom.xml 中声明的排除库/jar给您build.gradle .

希望有帮助。

关于java - Hibernate 抽象方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367077/

相关文章:

java - Map<String, List<String>> 如果键与字符串形式相同,则在运行时将值添加到列表中

运行 Hadoop 作业时出现 java.lang.OutOfMemoryError

java - 异常: org. hibernate.AnnotationException : No identifier specified for entity: jpa.租户.DataSourceConfig

spring - GET 对我的表单和 UTF-8 的请求

java - JavaExe下Windows服务启动失败: Updated

javascript - 如何在java中将NCPDP标准格式消息转换为xml格式?

hibernate - session 与 session 工厂之间的区别 - Hibernate?

java - 自动提交和@Transactional 以及与 spring、jpa 和 hibernate 的级联

java - 使用maven的spring hibernate集成中的org.springframework.beans.factory.BeanCreationException

java - 在服务层做一个 STATIC 方法有什么害处——Spring 3