1

我正在尝试使用嵌入式 h2 设置一个非常简单的 ormlite 堆栈。该应用程序由于NoSuchMethodErrorin ormlite 而崩溃:

Caused by: java.lang.NoSuchMethodError: com.j256.ormlite.db.DatabaseType.setDriver(Ljava/sql/Driver;)V
    at com.j256.ormlite.jdbc.JdbcConnectionSource.initialize(JdbcConnectionSource.java:133)
    at com.j256.ormlite.jdbc.JdbcConnectionSource.<init>(JdbcConnectionSource.java:112)
    at com.j256.ormlite.jdbc.JdbcConnectionSource.<init>(JdbcConnectionSource.java:70)
    at com.heyworld.resteasy.repositories.LocomotivesRepository.<init>(LocomotivesRepository.java:20)

提出 ormlite 的来源说databaseType.loadDriver();有一个未经处理的ClassNotFound异常并且databaseType.setDriver(DriverManager.getDriver(url));(它实际抱怨的行)无法解决方法setDriver()

我在这里有什么明显的遗漏吗?也许缺少类路径依赖项?

这是我的存储库:

package heyworld.resteasy.repositories;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.db.H2DatabaseType;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import heyworld.resteasy.model.Locomotive;

import java.sql.SQLException;
import java.util.List;

public class LocomotivesRepository {
    private Dao<Locomotive, Integer> locomotiveDao;
    String databaseUrl = "jdbc:h2:mem:locomotives";

    public LocomotivesRepository() {
        ConnectionSource connectionSource = null;
        try {
            connectionSource = new JdbcConnectionSource(databaseUrl, new H2DatabaseType());
             locomotiveDao = DaoManager.createDao(connectionSource, Locomotive.class);

        } catch (SQLException e) {
            e.printStackTrace();  
        }
    }

    public List<Locomotive> list() {
        try {
            return locomotiveDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            return list();
        }
    }
}

我的build.gradle

apply plugin: "war"
apply plugin: "idea"
apply plugin: "tomcat"
apply plugin: 'h2'


buildscript {
    repositories {
        mavenCentral()
        add(new org.apache.ivy.plugins.resolver.URLResolver()) {
            name = 'GitHub'
            addArtifactPattern 'http://cloud.github.com/downloads/[organisation]/[module]/[module]-[revision].[ext]'
        }
    }

    dependencies {
        classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
        classpath 'jamescarr:h2-gradle-plugin:0.8.2'
        classpath 'com.h2database:h2:1.3.164'
    }
}

repositories {
    maven {
        url "http://repository.jboss.org/maven2"
    }
    maven {
        url "https://repository.jboss.org/nexus/content/repositories/thirdparty-releases"
    }
    mavenCentral()
}

dependencies {
    providedCompile "javax.servlet:javax.servlet-api:3.0.1"
    compile "org.jboss.resteasy:resteasy-jaxrs:2.3.5.Final"
    compile "org.jboss.resteasy:resteasy-jackson-provider:2.3.5.Final"
    compile "org.jboss.resteasy:resteasy-guice:3.0-rc-1"
    compile "com.j256:ormlite:3.0"
    compile "com.j256.ormlite:ormlite-jdbc:4.45"
    compile "com.j256.ormlite:ormlite-core:4.45"
    compile "com.google.inject:guice:3.0"

    def tomcatVersion = '7.0.39'
    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'
    }
}

tomcatRun.doFirst {
    h2start.execute()
}

h2 {
    tcpPort = 9092
    webPort = 8082

    example {
        scripts = [
                'src/test/resources/locomotives.sql'
        ]
    }
}
4

1 回答 1

3

知道了。两者兼而有之compile "com.j256:ormlite:3.0"compile "com.j256.ormlite:ormlite-core:4.45"似乎把事情搞砸了。删除compile "com.j256:ormlite:3.0"修复它。

于 2013-06-12T13:29:53.647 回答