[英]Create dynamic primary key constraint with JOOQ
I am using JOOQ(newbie in JOOQ) to create the database at runtime using Rest API in my spring boot project.我正在使用 JOOQ(JOOQ 中的新手)在我的 spring 引导项目中使用 Rest API 在运行时创建数据库。 In one of the case I need to create a table with a composite primary key which can be combination of multiple columns.
在一种情况下,我需要创建一个具有复合主键的表,该主键可以是多列的组合。 I am using the below piece of code to create constraint -
我正在使用下面的代码来创建约束 -
ArrayList<Constraint> constraints = new ArrayList<>();
constraints.add(constraint(name("pk_" + tableName))
.primaryKey(field("column1"), field("column2")));
I already have List<Field<?>>
which will be working as the composite primary key.我已经有
List<Field<?>>
将用作复合主键。 How can I make this dynamic as the primary key constraint can support n number of columns?由于主键约束可以支持 n 列,我怎样才能使其动态化? Is there any way I can directly provide the field list in the
.primarykey()
API?有什么方法可以直接在
.primarykey()
API 中提供字段列表?
The usual overloads for primaryKey(Collection<? extends Field<?>>)
that one may expect in such a case are missing from the constraint API.约束 API 缺少在这种情况下可能预期的
primaryKey(Collection<? extends Field<?>>)
的通常重载。 I've create a feature request for jOOQ 3.15: https://github.com/jOOQ/jOOQ/issues/11816我已经为 jOOQ 3.15 创建了一个功能请求: https://github.com/jOOQ/jOOQ/issues/11816
You can just use ConstraintTypeStep.primaryKey(Field<?>...)
using standard JDK Collection.toArray()
methods:您可以使用标准 JDK
Collection.toArray()
方法使用ConstraintTypeStep.primaryKey(Field<?>...)
:
List<Field<?>> list = ...
// Pre Java 11
constraints.add(constraint(...).primaryKey(list.toArray(new Field<?>[0])));
// Using new JDK 11 API
constraints.add(constraint(...).primaryKey(list.toArray(Field<?>[]::new)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.