我正在尝试使用嵌入式h2设置一个非常简单的ormlite堆栈。应用程序因ormlite中的NoSuchMethodError
而爆炸:
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'
]
}
}
答案 0 :(得分:3)
知道了。同时拥有compile "com.j256:ormlite:3.0"
和compile "com.j256.ormlite:ormlite-core:4.45"
似乎搞得一团糟。删除compile "com.j256:ormlite:3.0"
修复它。