【问题标题】:Error creating entityManagerFactory due to error tying to scan <jar-file>由于绑定扫描 <jar-file> 时出错,创建 entityManagerFactory 时出错
【发布时间】:2014-06-18 08:56:30
【问题描述】:

我关注http://spring.io/guides/tutorials/data/3;我不是 确定我做错了什么,但我不断收到我不理解的异常。我尝试搜索具有相同例外的问题无济于事。

堆栈跟踪: http://pastebin.com/WYPqS6da

PersistenceConfig.java

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class PersistenceConfig {

    @Bean
    public DataSource dataSource() throws SQLException {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.HSQL).build();
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() throws SQLException {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.HSQL);
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.scrumster.persistence.domain");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();

        return factory.getObject();
    }

    @Bean
    public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
        return entityManagerFactory.createEntityManager();
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }
}

build.gradle:

apply plugin: 'war'
apply plugin: 'tomcat'
apply plugin: 'java'
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-eclipse'
apply plugin: 'eclipse'
apply plugin: 'idea'

buildscript {
  repositories {
    mavenCentral()
    maven {
      url "http://download.java.net/maven/2"
    }
    maven { url 'http://repo.spring.io/plugins-release' }
  }

  dependencies {
    classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
    classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.1'
  }
}

repositories {
  mavenCentral()
  maven { url 'http://repo.spring.io/milestone/'}
}

dependencies {
    def tomcatVersion = '7.0.42'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
      exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
    }

    compile 'org.springframework:spring-webmvc:4.0.5.RELEASE'
    compile 'org.springframework.data:spring-data-jpa:1.3.4.RELEASE'
    compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final'
    compile 'org.hibernate:hibernate-entitymanager:4.0.1.Final'
    compile 'org.springframework.hateoas:spring-hateoas:0.7.0.RELEASE'
    compile 'com.jayway.jsonpath:json-path:0.8.1'

    compile 'org.springframework.security:spring-security-web:3.2.0.M2'
    compile 'org.springframework.security:spring-security-core:3.2.0.M2'
    compile 'org.springframework.security:spring-security-config:3.2.0.M2'

    compile 'org.slf4j:slf4j-api:1.7.5'
    runtime 'org.hsqldb:hsqldb:2.3.2'
    runtime 'org.slf4j:slf4j-jdk14:1.7.5'
    runtime 'com.fasterxml.jackson.core:jackson-databind:2.3.3'
    runtime 'javax.xml.bind:jaxb-api:2.2.9'

    provided 'javax.servlet:javax.servlet-api:3.0.1'

    testCompile 'com.jayway.jsonpath:json-path-assert:0.8.1'
    testCompile 'org.springframework:spring-test:4.0.5.RELEASE'
    testCompile 'junit:junit:4.11'
    testCompile "org.mockito:mockito-core:1.9.5"

}

task wrapper(type: Wrapper) {
    gradleVersion = '1.12'
}

tomcatRunWar.contextPath = ''

堆栈跟踪:

Caused by: java.lang.RuntimeException: Error while reading file:/E:/Files/Source/Workspace-Eclipse2/scrumster/bin/
        at org.hibernate.ejb.packaging.NativeScanner.getFilesInJar(NativeScanner.java:193)
        at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:503)
        at org.hibernate.ejb.Ejb3Configuration.scanForClasses(Ejb3Configuration.java:851)
        ... 58 more
Caused by: java.io.IOException: invalid constant type: 18
        at javassist.bytecode.ConstPool.readOne(ConstPool.java:1113)
        at javassist.bytecode.ConstPool.read(ConstPool.java:1056)
        at javassist.bytecode.ConstPool.<init>(ConstPool.java:150)
        at javassist.bytecode.ClassFile.read(ClassFile.java:765)
        at javassist.bytecode.ClassFile.<init>(ClassFile.java:109)

我希望有人能指出正确的来源,或者帮助我解决这个困境。

【问题讨论】:

    标签: java spring gradle


    【解决方案1】:

    错误:

    invalid constant type: 18
    

    表示您已使用 Java 8 构建了 Jars,但正尝试在较低版本中运行应用程序。

    根据我在其他地方看到的情况,您可能需要切换到较新版本的 javassist(这会引发错误),因为您使用的版本与 Java 8 不兼容。

    【讨论】:

    • 查看我的附加编辑 - 问题不在于运行时,而在于您使用的是不兼容的库 (javassist)。它进行字节码操作,因此它对 JVM 版本比大多数更敏感。
    • 我怀疑它是由 build.gradle 中的一个依赖项引入的传递依赖项。我知道 Eclipse pom.xml 编辑器将显示依赖项的来源。不幸的是我不经常使用 Gradle,所以我不确定是否提供了类似的工具。
    • 我尝试将项目的合规级别降低到 Java 7(而不是更新我的一些东西),但仍然不适合我。
    • 同样的错误?您提供的堆栈跟踪告诉我 E:/Files/Source/Workspace-Eclipse2/scrumster/bin/ 中的某些内容是使用 Java 8 构建的。
    • 似乎将其转换为 Java 7 可以解决问题。谢谢。
    【解决方案2】:

    我在将我的一个项目迁移到 Java 8 时遇到了同样的问题。通过将 hibernate-entitymanager 工件版本从 4.2.0.Final 更新到 4.3.8.Final 来修复它。

    【讨论】:

      【解决方案3】:

      如果您使用 3.6.10-Final,则需要排除 javassist(不带 org. 前缀)

          <dependency>
                  <groupId>org.hibernate</groupId>
                  <artifactId>hibernate-entitymanager</artifactId>
                  <version>3.6.10.Final</version>
                  <scope>compile</scope>
                  <exclusions>
                      <exclusion>
                          <artifactId>javassist</artifactId>
                          <groupId>javassist</groupId>
                      </exclusion>
                  </exclusions>
              </dependency>
             <!-- javassist with fixed https://issues.jboss.org/browse/JASSIST-174 -->
             <dependency>
                  <groupId>org.javassist</groupId>
                  <artifactId>javassist</artifactId>
                  <version>3.18.2-GA</version>
              </dependency>
      

      【讨论】:

        【解决方案4】:

        您不需要为 EntityManagerFactory 和 EntityManager 提供 bean。 只需提供一个 bean LocalContainerEntityManagerFactoryBean

        【讨论】:

        • 嗨,我对 Spring 很陌生。你能解释一下你的答案吗?
        【解决方案5】:

        我有类似的问题。我的问题是我有另一个使用不同版本的 javassist 的依赖项(在我的情况下是 jbossweld)。排除没有帮助。

            <dependency>
                <groupId>org.jboss.weld</groupId>
                <artifactId>weld-se</artifactId>
                <version>1.0.1-Final</version>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <artifactId>javassist</artifactId>
                        <groupId>org.javassist</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
        

        因此,上述排除不起作用。我必须为 jboss Weld 找到解决方法。

        【讨论】:

          猜你喜欢
          • 2020-02-08
          • 1970-01-01
          • 1970-01-01
          • 2019-03-13
          • 2013-06-15
          • 2020-07-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多