在sqlite上,Jooq在商店之后没有返回主键

时间:2018-05-07 15:35:45

标签: java sql jooq

我正在使用Gradle和https://github.com/etiennestuder/gradle-jooq-plugin,配置如此

jooq {
    version = '3.10.6'
    foo(sourceSets.main) {
        jdbc {
            url = "jdbc:sqlite:${projectDir.absolutePath}/foo.sqlite"
        }
    }
}

我有以下感兴趣的依赖

jooqRuntime 'org.xerial:sqlite-jdbc:3.21.0.1'
compile 'org.xerial:sqlite-jdbc:3.21.0.1'

我的foo DB包含以下内容

CREATE TABLE tree (
  id              INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  grove_id        INTEGER,
  type            INTEGER NOT NULL,
  latitude        FLOAT   NOT NULL,
  longitude       FLOAT   NOT NULL,
  FOREIGN KEY (grove_id) REFERENCES grove (id)
  ON DELETE CASCADE
  ON UPDATE CASCADE
);
CREATE TABLE grove (
  id            INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
);

在创建新的客户端树时,我希望使用'记录'在SQLite数据库中保留它。办法。所以我正在做

DSLContext dslContext = DslContext.get("jdbc:sqlite:"+path_to_sqlite_file);
TreeRecord treeRecord = dslContext.newRecord(TREE);
treeRecord.setGroveId(10);
treeRecord.setType(1);
treeRecord.setLatitude(0.1);
treeRecord.setLongitude(0.2);
treeRecord.store(); // this works, when inspecting the SQLite file afterwards
treeRecord.getId(); => this is null, even though the DB correctly has a ID value attributed.

在SQLite数据库中,我没有找到任何告诉Jooq不支持此类功能的内容。不是吗?

1 个答案:

答案 0 :(得分:0)

好的,问题是由于我的DslContext单例,它是:

import org.jooq.Configuration;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.sqlite.SQLiteDataSource;

public class DslContext {

    private static org.jooq.DSLContext INSTANCE;

    public static org.jooq.DSLContext get(String dbUrl) {
        if (INSTANCE == null) {
            INSTANCE = instantiate(dbUrl);
        }
        return INSTANCE;
    }

    private static org.jooq.DSLContext instantiate(String dbUrl) {
        SQLiteDataSource ds = new SQLiteDataSource();
        ds.setUrl(dbUrl);
        Configuration configuration = new DefaultConfiguration()
                .set(SQLDialect.SQLITE)
                .set(new DataSourceConnectionProvider(ds));
        return DSL.using(configuration);
    }

    private DslContext() {
    }

}

不确定为什么这不起作用。 我回过头来在我的代码段中使用DSL.using("jdbc:sqlite:"+path_to_sqlite_file);而不是DslContext.get("jdbc:sqlite:"+path_to_sqlite_file);

相关问题