[英]Hibernate AbstractMethodError
我在gradle項目上使用的是spring,hibernate和spring數據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 config類,它從未給我舊版本的工件帶來問題
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不兼容引起的。 我嘗試了自己的gradle構建,並通過查看您的WAR文件發現了可能導致此問題的內容:
您使用的是舊版本的hibernate-envers。 您不是要這樣做,而是聲明依賴於spring-data-envers(因此spring-data-envers需要休眠-envers JAR),這很可能會引起問題。
我看到您重復了bcmail-jdk14-138.jar,bcprov-jdk14-138.jar和stax-api-1.0.x.jar。 刪除其中之一可能會解決您的問題。
編輯:這些罐子中的一些來自名為傳遞依賴的事物,這在maven和gradle中都是非常常見和自然的。 當不同的庫/框架與具有不同版本的另一個庫具有相同的依存關系時,就會發生沖突。
例如,hibernate和spring對cglib的依賴關系相同。 但是hibernate聲明它們依賴於cglib-1.2,而spring決定依賴於cglib-1.0。 最后,當您構建WAR(或具有相關性的JAR)時,您的構建將同時帶來cglib-1.0和cglib-1.2。
我如何擺脫他們
您可以嘗試執行:
gradle dependencies
或使用提供的任何變體 ,然后在build.gradle中排除重復的jar並聲明依賴項,如下所示:
compile ('com.lowagie:itext:2.1.7') {
exclude group: 'org.bouncycastle', module: 'bcmail-jdk14'
}
如果發現您不習慣使用命令行來區分重復的依賴項,則可以嘗試:
在您的build.gradle中添加Apply apply plugin: "maven"
,然后執行gradle install
命令。
在<project-root-dir>/build
,將pom-default.xml復制到<project-root-dir>/pom.xml
。
使用支持m2eclipse的eclipse打開該項目(我認為所有最新的eclipse版本都應支持maven)。
打開pom.xml並將選項卡更改為“ Dependency Hierarchy”,然后可以在右側面板中“排除”依賴項。 在pom.xml
,排除的依賴項將標記為
<exclusion> .... </exclusion>
將已在pom.xml
聲明的排除的libs / jars轉換為build.gradle
。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.