Is there any way to return the id of that last inserted record other than dslContext.lastID() in JOOQ?

huangapple go评论62阅读模式
英文:

Is there any way to return the id of that last inserted record other than dslContext.lastID() in JOOQ?

问题

I need to retrieve the ID of the last inserted record, so I can insert a value like 'ABCD-lastId' into a field for unique identification, in addition to the primary key. Are there any alternative methods for achieving this?

Currently I'm using last ID method of DSL Context for getting the id. But I'm not sure it is a good approach since at the same time some other record are inserted.

I'm using like this

dslContext.insertInto(table(name), field("email"), field("password"))
                    .values("test@mail.com", "password")
                    .execute();
System.out.println(dslContext.lastID());
英文:

I need to retrieve the ID of the last inserted record, so I can insert a value like 'ABCD-lastId' into a field for unique identification, in addition to the primary key. Are there any alternative methods for achieving this?

Currently I'm using last ID method of DSL Context for getting the id. But I'm not sure it is a good approach since at the same time some other record are inserted.

I'm using like this

dslContext.insertInto(table(name), field("email"), field("password"))
                .values("test@mail.com", "password")
                .execute();
        System.out.println(dslContext.lastID());

答案1

得分: 1

以下是翻译的内容:

如何在 PostgreSQL 中使用 jOOQ 返回生成的 ID:

只需在您的 INSERT 语句中添加一个 RETURNING 子句。对于 PostgreSQL,jOOQ 支持原生操作:

Record result =
dslContext.insertInto(table(name), field("email"), field("password"))
                .values("test@mail.com", "password")
                .returningResult(field("guid"))
                .fetchOne();
System.out.println(result);

关于使用代码生成器的说明:

通常建议在几乎所有情况下使用 jOOQ 的代码生成器(除非您有一个动态模式)。您将获得编译时类型安全性和许多其他重要好处,其中包括您可以使用 jOOQ 的 UpdatableRecord 简化 CRUD。默认情况下,UpdatableRecord 会自动为您获取生成的键:

MyTableRecord r = ctx.newRecord(MY_TABLE);
r.setEmail("test@gmail.com");
r.setPassword("password");
r.store();
System.out.println(r.getGuid()); // 这已自动获取

多租户模式的模式映射

您使用了 table(name),这暗示了动态模式,但请注意,如果您的模式之所以是动态的唯一原因是多租户,那么请注意 jOOQ 有一个称为 catalog、schema 和 table 映射 的解决方案。

英文:

How to return generated IDs with jOOQ in PostgreSQL:

Just add a RETURNING clause to your INSERT statement. For PostgreSQL, this is supported natively by jOOQ:

Record result =
dslContext.insertInto(table(name), field("email"), field("password"))
                .values("test@mail.com", "password")
                .returningResult(field("guid"))
                .fetchOne();
System.out.println(result);

A note on using the code generator:

It's generally recommended to use jOOQ's code generator for almost all cases (except when you have a dynamic schema). You'll get compile time type safety and a lot of other great benefits, among others, you can use jOOQ's UpdatableRecord for simplified CRUD. The UpdatableRecord, by default, fetches generated keys for you, automatically:

MyTableRecord r = ctx.newRecord(MY_TABLE);
r.setEmail("test@gmail.com");
r.setPassword("password");
r.store();
System.out.println(r.getGuid()); // this has been fetched automatically

Schema mapping for multi tenancy

You're using table(name), which hints at a dynamic schema, but do note that if the only reason your schema is dynamic is multi tenancy, then please note that jOOQ has a solution for that called catalog, schema, and table mapping

huangapple
  • 本文由 发表于 2023年4月17日 19:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定