java - Jooq 在 sqlite 上存储后不返回主键

标签 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 数据库包含以下内容

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.

我没有发现任何信息表明 Jooq 在 SQLite 数据库上不支持此类功能。不是吗?

最佳答案

好吧,问题是由于我的 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);

关于java - Jooq 在 sqlite 上存储后不返回主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50217820/

相关文章:

mysql - 如何将一行添加到 mysql 结果集以用作 html 选择框中的提示?

java - 表格 reshape

java - 如何从 SQL 查询中获取列名(Jooq,Java)

java - 如何通过 jOOQ 代码生成器和 Maven 使用自定义策略?

java - Spring Security SAML 插件 - 未配置托管服务提供商异常

java - MySQL JDBC 插件加载

mysql - 最后一分钟每个表的数据

java - 如何在没有实体和 JPA 存储库的情况下在 Spring 中运行 native SQL 查询?

java - 使用 PowerMock 和 Robolectric - IncompatibleClassChangeError

Java:从 JTextField 获取用户输入