Maven 3.2.1 + Hibernate 4.3.4 - 无法构建实体管理器工厂 - 引起:ParameterizedTypeImpl无法强制转换为java.lang.Class

时间:2014-03-17 10:13:14

标签: java hibernate jpa maven-2 log4j

我已将Hibernate 4.3.4迁移到Maven 3.2.1项目。我用maven-shade-plugin创建了一个jar文件,但是当我启动它时,我有以下异常:

Mar 17, 2014 10:44:35 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider               [org.hibernate.ejb.HibernatePersistence]; use     [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Mar 17, 2014 10:44:35 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Mar 17, 2014 10:44:35 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider     [org.hibernate.ejb.HibernatePersistence]; use     [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Mar 17, 2014 10:44:35 AM org.hibernate.jpa.internal.util.LogHelper     logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: MfsDao
    ...]
Mar 17, 2014 10:44:36 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
Mar 17, 2014 10:44:36 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties:     {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false,     hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
Mar 17, 2014 10:44:36 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 17, 2014 10:44:44 AM     org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}

javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:81)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at de.cegelec.vwezlt.mfs.dao.MfsEntityManager.getEntityManagerFactory(MfsEntityManager.java:117)
at de.cegelec.vwezlt.mfs.dao.MfsEntityManager.getMfsEntityManager(MfsEntityManager.java:25)
at de.cegelec.vwezlt.mfsvert.main.MfsVert.main(MfsVert.java:168)

Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class
at org.hibernate.cfg.AttributeConverterDefinition.<init>(AttributeConverterDefinition.java:67)
at org.hibernate.cfg.Configuration.addAttributeConverter(Configuration.java:2690)
at org.hibernate.cfg.Configuration.addAttributeConverter(Configuration.java:2641)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.buildHibernateConfiguration(EntityManagerFactoryBuilderImpl.java:1129)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:846)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:73)

我已经调试过,我发现引发异常的类是org.apache.logging.log4j.core.appender.db.jpa.converter.ContextMapAttributeConverter

在我的pom中,我依赖log4j 2.0-rc1作为依赖:

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-rc1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-rc1</version>
  </dependency>

在我尝试将所有内容集成到Maven之前,它运行良好。

有什么想法吗?

提前致谢!

4 个答案:

答案 0 :(得分:6)

正如aaronfc已经提到的,hibernate JIRA上有一张开放票:https://hibernate.atlassian.net/browse/HHH-8804

所以问题是与Log4j2转换器碰撞,更详细的是db appender JPA转换器。

然而,只有“胖”罐才会出现问题...

解决方法:如果您使用 maven-shade-plugin 打包jar - 并且不需要任何log4j2 db appender,则可以排除碰撞的Converter类:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>jar-with-dependencies</shadedClassifierName> 
                <filters>
                    <filter>
                        <artifact>org.apache.logging.log4j:*</artifact>
                        <excludes>
                            <exclude>org/apache/logging/log4j/core/appender/db/jpa/converter/*</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 1 :(得分:1)

我也偶然发现了这一点。 https://hibernate.atlassian.net/browse/HHH-8804

上有一张开放票

我通过降级到Hibernate 4.2.12.Final和JPA2.0来“改变”它。希望它有所帮助!

编辑: 对不起,没看到这是一个两个月大的帖子,无论如何希望它对某人有用:P

答案 2 :(得分:1)

在扩展到@hartmut的答案时,如果您使用的是Gradle,请添加Gradle Shadow,并在build.gradle中执行以下操作:

shadowJar {
    exclude 'org/apache/logging/log4j/core/appender/db/jpa/converter/*'
}

然后,您可以运行gradle shadowJar来创建jar

答案 3 :(得分:0)

我找到了一个有效的解决方法。而不是创建一个“胖”jar,我用我的项目创建了一个Jar,并在一个单独的“lib”文件夹中创建了一个依赖项(当然包括Hibernate)。

我不知道,但它现在有效。