Hibernate错误 - 无法构建ClassFile

时间:2018-06-18 17:19:40

标签: java hibernate

所以我在这里走到了尽头。现在已经进行了半天的故障排除。在Java应用程序中使用Hibernate JPA持久性。

在IDE(IntelliJ 2018.1.5)中运行代码时运行正常,但是,当尝试通过命令行从jar运行时,我收到以下错误(完整堆栈跟踪):

Exception in thread "main" org.hibernate.boot.archive.spi.ArchiveException: Could not build ClassFile
        at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.toClassFile(ClassFileArchiveEntryHandler.java:64)
        at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.handleEntry(ClassFileArchiveEntryHandler.java:47)
        at org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor.visitArchive(JarFileBasedArchiveDescriptor.java:147)
        at org.hibernate.boot.archive.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:47)
        at org.hibernate.boot.model.process.internal.ScanningCoordinator.coordinateScan(ScanningCoordinator.java:75)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:98)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:228)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:170)
        at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:181)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:129)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
        at com.gsr.metrics.repository.BaseRepository.<init>(BaseRepository.java:10)
        at com.gsr.metrics.repository.ProcessHistoryRepository.<init>(ProcessHistoryRepository.java:11)
        at com.gsr.metrics.FileProcessor.<init>(FileProcessor.java:24)
        at com.gsr.metrics.PostProcessor.run(PostProcessor.java:42)
        at com.gsr.metrics.PostProcessor.main(PostProcessor.java:28) Caused by: java.io.IOException: invalid constant type: 19 at 5
        at javassist.bytecode.ConstPool.readOne(ConstPool.java:1241)
        at javassist.bytecode.ConstPool.read(ConstPool.java:1172)
        at javassist.bytecode.ConstPool.<init>(ConstPool.java:185)
        at javassist.bytecode.ClassFile.read(ClassFile.java:807)
        at javassist.bytecode.ClassFile.<init>(ClassFile.java:148)
        at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.toClassFile(ClassFileArchiveEntryHandler.java:61)    
 ... 19 more

构建配置是Maven,这是Hibernate依赖项

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gsr.metrics</groupId>
    <artifactId>PostProcessor</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.gsr.metrics.PostProcessor</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.12.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.4</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>com.beust</groupId>
            <artifactId>jcommander</artifactId>
            <version>1.72</version>
        </dependency>
    </dependencies>

</project>

我尝试了不同的Hibernate版本,但在所有情况下都遇到了同样的错误。

执行此语句时出现问题

em = Persistence.createEntityManagerFactory(persistenceUnitName).createEntityManager();

2 个答案:

答案 0 :(得分:1)

经过大量的试验和错误,我将问题隔离到与log4j某种类型的库冲突。不要问我确切的问题是什么,但是更改两者之一的版本会使问题消失。没有时间对根本原因进行任何形式的详细分析。

答案 1 :(得分:1)

我偶然发现了同样的问题,并且找到了类似的解决方案。 我使用的是log4j-core 2.11.1,从jar运行应用程序时遇到了相同的错误。我已经按照您的解决方案将版本更改为log4j-core 2.8.2。 在那之后,我仍然有一个错误,但是这次是缺乏依赖性的:显然它缺少“ com.fasterxml.jackson.core”包类之一。因此,我添加了依赖项(版本2.9.6),现在它正在运行。

希望这对某些人有帮助,因为这是一个非常令人沮丧的错误。