Jooq not returning primary key after store, on sqlite

前提是你 提交于 2019-12-11 07:44:14

问题


I'm using Gradle and https://github.com/etiennestuder/gradle-jooq-plugin, configured like that

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

I have the following dependencies of interest

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

My foo DB contains the following

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,
);

When creating a new tree client-side, I want to persist it in my SQLite DB using the 'record' way. So I'm doing

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.

I didn't find anything that tells this type of feature is not supported by Jooq on SQLite db. Isn't it ?


回答1:


Ok, the problem was due to my DslContext singleton, which was:

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() {
    }

}

Not sure why this was not working. I felt back to using DSL.using("jdbc:sqlite:"+path_to_sqlite_file); in my code snippet instead of DslContext.get("jdbc:sqlite:"+path_to_sqlite_file);



来源:https://stackoverflow.com/questions/50217820/jooq-not-returning-primary-key-after-store-on-sqlite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!