英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论