如果第一行的bigint
是boolean
,则jOOQ 3.10.7在多行upsert中选择了错误的类型(boolean
而不是null
。 / p>
下面是构建和执行查询的通用代码。
try (Connection writeConn = DatabaseManager.getConnection(true);
final DSLContext writeContext = DatabaseManager.getBuilder(writeConn);
InsertQuery<UserProgramAssignmentsRecord> programAssignmentsUpdateQuery = writeContext
.insertQuery(AccessControlWriteDAO.userProgramAssignments)) {
programAssignments
.forEach(assignment -> programAssignmentsUpdateQuery.addRecord(assignment));
programAssignmentsUpdateQuery.addValuesForUpdate(UserProgramAssignment.getExcludedMap());
programAssignmentsUpdateQuery
.addConditions(UserProgramAssignment.getUpsertUpdateCondition());
programAssignmentsUpdateQuery.execute();
}
}
生成的查询(内联数据)为:
insert into "user_program_assignments" (
"program_id",
"create_projects",
"justification",
"modified_by",
"modified_date",
"user_id",
"approve_all_child_projects",
"edit_all_draft_child_projects",
"moderate_all_child_projects",
"view_all_draft_child_projects",
"administer"
)
values (
18792,
true,
'Permissions Moderator access added per xxx.',
'24',
timestamp '2018-06-22 08:19:44.977',
469882,
true,
true,
false,
true,
null
), (
18792,
true,
'Adding additional authority for this program/project',
'24',
timestamp '2018-06-22 08:19:44.977',
78116,
true,
true,
false,
true,
null
), (
18792,
false,
'Resetting for new programs/projects',
'24',
timestamp '2018-06-22 08:19:44.977',
97395,
false,
true,
false,
true,
null
), (
18792,
true,
'Updating to new program/project structure',
'24',
timestamp '2018-06-22 08:19:44.977',
135253,
true,
true,
false,
true,
true
), (
18792,
true,
'Will be assisting with ARMD data entry.',
'24',
timestamp '2018-06-22 08:19:44.977',
296287,
true,
true,
false,
true,
null
), (
18792,
true,
'Resetting for new programs/projects',
'24',
timestamp '2018-06-22 08:19:44.977',
111237,
false,
false,
false,
true,
null
), (
18792,
true,
'TEST',
'24',
timestamp '2018-06-22 08:19:44.977',
237927,
true,
true,
false,
true,
null
), (
18792,
true,
'TEST',
'24',
timestamp '2018-06-22 08:19:44.977',
520822,
true,
true,
false,
true,
null
), (
18792,
true,
'TEST',
'24',
timestamp '2018-06-22 08:19:44.977',
554733,
true,
true,
false,
true,
true
)
on conflict (
"program_id",
"user_id"
) do update
set "approve_all_child_projects" = "excluded"."approve_all_child_projects",
"create_projects" = "excluded"."create_projects",
"edit_all_draft_child_projects" = "excluded"."edit_all_draft_child_projects",
"view_all_draft_child_projects" = "excluded"."view_all_draft_child_projects",
"modified_by" = "excluded"."modified_by",
"administer" = "excluded"."administer",
"justification" = "excluded"."justification",
"moderate_all_child_projects" = "excluded"."moderate_all_child_projects",
"modified_date" = "excluded"."modified_date"
where (
"user_program_assignments"."approve_all_child_projects" <> "excluded"."approve_all_child_projects"
or (
"user_program_assignments"."approve_all_child_projects" is null
and "excluded"."approve_all_child_projects" is not null
)
or (
"user_program_assignments"."approve_all_child_projects" is not null
and "excluded"."approve_all_child_projects" is null
)
or "user_program_assignments"."create_projects" <> "excluded"."create_projects"
or (
"user_program_assignments"."create_projects" is null
and "excluded"."create_projects" is not null
)
or (
"user_program_assignments"."create_projects" is not null
and "excluded"."create_projects" is null
)
or "user_program_assignments"."edit_all_draft_child_projects" <> "excluded"."edit_all_draft_child_projects"
or (
"user_program_assignments"."edit_all_draft_child_projects" is null
and "excluded"."edit_all_draft_child_projects" is not null
)
or (
"user_program_assignments"."edit_all_draft_child_projects" is not null
and "excluded"."edit_all_draft_child_projects" is null
)
or "user_program_assignments"."moderate_all_child_projects" <> "excluded"."moderate_all_child_projects"
or (
"user_program_assignments"."moderate_all_child_projects" is null
and "excluded"."moderate_all_child_projects" is not null
)
or (
"user_program_assignments"."moderate_all_child_projects" is not null
and "excluded"."moderate_all_child_projects" is null
)
or "user_program_assignments"."view_all_draft_child_projects" <> "excluded"."view_all_draft_child_projects"
or (
"user_program_assignments"."view_all_draft_child_projects" is null
and "excluded"."view_all_draft_child_projects" is not null
)
or (
"user_program_assignments"."view_all_draft_child_projects" is not null
and "excluded"."view_all_draft_child_projects" is null
)
or "user_program_assignments"."justification" <> "excluded"."justification"
or (
"user_program_assignments"."justification" is null
and "excluded"."justification" is not null
)
or "user_program_assignments"."administer" <> "excluded"."administer"
or (
"user_program_assignments"."administer" is null
and "excluded"."administer" is not null
)
or (
"user_program_assignments"."administer" is not null
and "excluded"."administer" is null
)
);
查询(未内联数据)为:
org.jooq.exception.DataAccessException: SQL [insert into "user_program_assignments" (
"program_id",
"create_projects",
"justification",
"modified_by",
"modified_date",
"user_id",
"approve_all_child_projects",
"edit_all_draft_child_projects",
"moderate_all_child_projects",
"view_all_draft_child_projects",
"administer"
)
values (
?,
?,
?,
?,
cast(? as timestamp),
?,
?,
?,
?,
?,
?
), (
?,
?,
?,
?,
cast(? as timestamp),
?,
?,
?,
?,
?,
?
), (
?,
?,
?,
?,
cast(? as timestamp),
?,
?,
?,
?,
?,
?
), (
?,
?,
?,
?,
cast(? as timestamp),
?,
?,
?,
?,
?,
?
), (
?,
?,
?,
?,
cast(? as timestamp),
?,
?,
?,
?,
?,
?
), (
?,
?,
?,
?,
cast(? as timestamp),
?,
?,
?,
?,
?,
?
), (
?,
?,
?,
?,
cast(? as timestamp),
?,
?,
?,
?,
?,
?
)
on conflict (
"program_id",
"user_id"
) do update
set "approve_all_child_projects" = "excluded"."approve_all_child_projects",
"create_projects" = "excluded"."create_projects",
"edit_all_draft_child_projects" = "excluded"."edit_all_draft_child_projects",
"view_all_draft_child_projects" = "excluded"."view_all_draft_child_projects",
"modified_by" = "excluded"."modified_by",
"administer" = "excluded"."administer",
"justification" = "excluded"."justification",
"moderate_all_child_projects" = "excluded"."moderate_all_child_projects",
"modified_date" = "excluded"."modified_date"
where (
"user_program_assignments"."approve_all_child_projects" <> "excluded"."approve_all_child_projects"
or (
"user_program_assignments"."approve_all_child_projects" is null
and "excluded"."approve_all_child_projects" is not null
)
or (
"user_program_assignments"."approve_all_child_projects" is not null
and "excluded"."approve_all_child_projects" is null
)
or "user_program_assignments"."create_projects" <> "excluded"."create_projects"
or (
"user_program_assignments"."create_projects" is null
and "excluded"."create_projects" is not null
)
or (
"user_program_assignments"."create_projects" is not null
and "excluded"."create_projects" is null
)
or "user_program_assignments"."edit_all_draft_child_projects" <> "excluded"."edit_all_draft_child_projects"
or (
"user_program_assignments"."edit_all_draft_child_projects" is null
and "excluded"."edit_all_draft_child_projects" is not null
)
or (
"user_program_assignments"."edit_all_draft_child_projects" is not null
and "excluded"."edit_all_draft_child_projects" is null
)
or "user_program_assignments"."moderate_all_child_projects" <> "excluded"."moderate_all_child_projects"
or (
"user_program_assignments"."moderate_all_child_projects" is null
and "excluded"."moderate_all_child_projects" is not null
)
or (
"user_program_assignments"."moderate_all_child_projects" is not null
and "excluded"."moderate_all_child_projects" is null
)
or "user_program_assignments"."view_all_draft_child_projects" <> "excluded"."view_all_draft_child_projects"
or (
"user_program_assignments"."view_all_draft_child_projects" is null
and "excluded"."view_all_draft_child_projects" is not null
)
or (
"user_program_assignments"."view_all_draft_child_projects" is not null
and "excluded"."view_all_draft_child_projects" is null
)
or "user_program_assignments"."justification" <> "excluded"."justification"
or (
"user_program_assignments"."justification" is null
and "excluded"."justification" is not null
)
or "user_program_assignments"."administer" <> "excluded"."administer"
or (
"user_program_assignments"."administer" is null
and "excluded"."administer" is not null
)
or (
"user_program_assignments"."administer" is not null
and "excluded"."administer" is null
)
)]; ERROR: column "administer" is of type boolean but expression is of type bigint
Hint: You will need to rewrite or cast the expression.
Position: 861
at org.jooq_3.10.7.POSTGRES_9_5.debug(Unknown Source) ~[?:?]
at org.jooq.impl.Tools.translate(Tools.java:2241) ~[jooq-3.10.7.jar:?]
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:704) ~[jooq-3.10.7.jar:?]
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:361) ~[jooq-3.10.7.jar:?]
at xxx.upsertCapabilities(xxx:467) ~[classes/:?]
...
Caused by: org.postgresql.util.PSQLException: ERROR: column "administer" is of type boolean but expression is of type bigint
Hint: You will need to rewrite or cast the expression.
Position: 861
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:144) ~[postgresql-42.2.2.jar:42.2.2]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44) ~[HikariCP-2.6.3.jar:?]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java) ~[HikariCP-2.6.3.jar:?]
at org.jooq.tools.jdbc.DefaultPreparedStatement.execute(DefaultPreparedStatement.java:209) ~[jooq-3.10.7.jar:?]
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:429) ~[jooq-3.10.7.jar:?]
at org.jooq.impl.AbstractDMLQuery.execute(AbstractDMLQuery.java:452) ~[jooq-3.10.7.jar:?]
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:347) ~[jooq-3.10.7.jar:?]
... 124 more
表的DDL为:
create table user_program_assignments
(
program_id bigint not null
constraint pgua_pg___fk
references programs
on delete cascade,
create_projects boolean,
justification varchar(4000),
modified_by varchar(30) not null,
modified_date timestamp default timezone('UTC'::text, now()) not null,
user_id bigint not null
constraint pgua_ui___fk
references users
on delete cascade,
administer boolean default false not null,
approve_all_child_projects boolean default false not null,
edit_all_draft_child_projects boolean default false not null,
moderate_all_child_projects boolean default false not null,
view_all_draft_child_projects boolean default false not null,
constraint user_program_assignments_pkey
primary key (program_id, user_id)
)
;
jOOQ生成的文件是(请注意3.11.1的文件是相同的,并且3.11.1使用此文件正常工作):
/*
* This file is generated by jOOQ.
*/
...
/**
* Table to store user permissions for a given program.
*/
@Generated(
value = {
"http://www.jooq.org",
"jOOQ version:3.10.7"
},
comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class UserProgramAssignments extends TableImpl<UserProgramAssignmentsRecord> {
private static final long serialVersionUID = -1670872896;
/**
* The reference instance of <code>user_program_assignments</code>
*/
public static final UserProgramAssignments USER_PROGRAM_ASSIGNMENTS = new UserProgramAssignments();
/**
* The class holding records for this type
*/
@Override
public Class<UserProgramAssignmentsRecord> getRecordType() {
return UserProgramAssignmentsRecord.class;
}
/**
* The column <code>user_program_assignments.program_id</code>. Foreign key reference to the Programs table. Combines with the user_id column to create the primary key.
*/
public final TableField<UserProgramAssignmentsRecord, Long> PROGRAM_ID = createField("program_id", org.jooq.impl.SQLDataType.BIGINT.nullable(false), this, "Foreign key reference to the Programs table. Combines with the user_id column to create the primary key.");
/**
* The column <code>user_program_assignments.create_projects</code>. If true, the user is able to create projects under the specified program.
*/
public final TableField<UserProgramAssignmentsRecord, Boolean> CREATE_PROJECTS = createField("create_projects", org.jooq.impl.SQLDataType.BOOLEAN, this, "If true, the user is able to create projects under the specified program.");
/**
* The column <code>user_program_assignments.justification</code>. The rationale for the user's level of permission assignments on the program.
*/
public final TableField<UserProgramAssignmentsRecord, String> JUSTIFICATION = createField("justification", org.jooq.impl.SQLDataType.VARCHAR(4000), this, "The rationale for the user's level of permission assignments on the program.");
/**
* The column <code>user_program_assignments.modified_by</code>. Standard column used to identify the person who created/modified the record.
*/
public final TableField<UserProgramAssignmentsRecord, String> MODIFIED_BY = createField("modified_by", org.jooq.impl.SQLDataType.VARCHAR(30).nullable(false), this, "Standard column used to identify the person who created/modified the record.");
/**
* The column <code>user_program_assignments.modified_date</code>. Standard column used to identify the when the record was created/last modified and should be tied to the modified_by field.
*/
public final TableField<UserProgramAssignmentsRecord, LocalDateTime> MODIFIED_DATE = createField("modified_date", org.jooq.impl.SQLDataType.LOCALDATETIME.nullable(false).defaultValue(org.jooq.impl.DSL.field("timezone('UTC'::text, now())", org.jooq.impl.SQLDataType.LOCALDATETIME)), this, "Standard column used to identify the when the record was created/last modified and should be tied to the modified_by field.");
/**
* The column <code>user_program_assignments.user_id</code>. Foreign key reference to the Users table. Combines with the program_id column to create the primary key.
*/
public final TableField<UserProgramAssignmentsRecord, Long> USER_ID = createField("user_id", org.jooq.impl.SQLDataType.BIGINT.nullable(false), this, "Foreign key reference to the Users table. Combines with the program_id column to create the primary key.");
/**
* The column <code>user_program_assignments.administer</code>. If true, the user is able to moderate, edit, and otherwise administer the specified program.
*/
public final TableField<UserProgramAssignmentsRecord, Boolean> ADMINISTER = createField("administer", org.jooq.impl.SQLDataType.BOOLEAN.nullable(false).defaultValue(org.jooq.impl.DSL.field("false", org.jooq.impl.SQLDataType.BOOLEAN)), this, "If true, the user is able to moderate, edit, and otherwise administer the specified program.");
/**
* The column <code>user_program_assignments.approve_all_child_projects</code>. 0/1 toggle flag identifying whether or not the user has permissions for approving all draft projects under this program.
*/
public final TableField<UserProgramAssignmentsRecord, Boolean> APPROVE_ALL_CHILD_PROJECTS = createField("approve_all_child_projects", org.jooq.impl.SQLDataType.BOOLEAN.nullable(false).defaultValue(org.jooq.impl.DSL.field("false", org.jooq.impl.SQLDataType.BOOLEAN)), this, "0/1 toggle flag identifying whether or not the user has permissions for approving all draft projects under this program.");
/**
* The column <code>user_program_assignments.edit_all_draft_child_projects</code>. 0/1 toggle flag identifying whether or not the user has permissions for editing all draft projects under this program.
*/
public final TableField<UserProgramAssignmentsRecord, Boolean> EDIT_ALL_DRAFT_CHILD_PROJECTS = createField("edit_all_draft_child_projects", org.jooq.impl.SQLDataType.BOOLEAN.nullable(false).defaultValue(org.jooq.impl.DSL.field("false", org.jooq.impl.SQLDataType.BOOLEAN)), this, "0/1 toggle flag identifying whether or not the user has permissions for editing all draft projects under this program.");
/**
* The column <code>user_program_assignments.moderate_all_child_projects</code>. 0/1 toggle flag identifying whether or not the user has permission to set user permissions on all projects under this program.
*/
public final TableField<UserProgramAssignmentsRecord, Boolean> MODERATE_ALL_CHILD_PROJECTS = createField("moderate_all_child_projects", org.jooq.impl.SQLDataType.BOOLEAN.nullable(false).defaultValue(org.jooq.impl.DSL.field("false", org.jooq.impl.SQLDataType.BOOLEAN)), this, "0/1 toggle flag identifying whether or not the user has permission to set user permissions on all projects under this program.");
/**
* The column <code>user_program_assignments.view_all_draft_child_projects</code>. 0/1 toggle flag identifying whether or not the user has permissions for viewing all draft projects under this program.
*/
public final TableField<UserProgramAssignmentsRecord, Boolean> VIEW_ALL_DRAFT_CHILD_PROJECTS = createField("view_all_draft_child_projects", org.jooq.impl.SQLDataType.BOOLEAN.nullable(false).defaultValue(org.jooq.impl.DSL.field("false", org.jooq.impl.SQLDataType.BOOLEAN)), this, "0/1 toggle flag identifying whether or not the user has permissions for viewing all draft projects under this program.");
/**
* Create a <code>user_program_assignments</code> table reference
*/
public UserProgramAssignments() {
this(DSL.name("user_program_assignments"), null);
}
/**
* Create an aliased <code>user_program_assignments</code> table reference
*/
public UserProgramAssignments(String alias) {
this(DSL.name(alias), USER_PROGRAM_ASSIGNMENTS);
}
/**
* Create an aliased <code>user_program_assignments</code> table reference
*/
public UserProgramAssignments(Name alias) {
this(alias, USER_PROGRAM_ASSIGNMENTS);
}
private UserProgramAssignments(Name alias, Table<UserProgramAssignmentsRecord> aliased) {
this(alias, aliased, null);
}
private UserProgramAssignments(Name alias, Table<UserProgramAssignmentsRecord> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, "Table to store user permissions for a given program.");
}
/**
* {@inheritDoc}
*/
@Override
public List<Index> getIndexes() {
return Arrays.<Index>asList(Indexes.PGUA_UIPI___INDEX, Indexes.USER_PROGRAM_ASSIGNMENTS_PKEY);
}
/**
* {@inheritDoc}
*/
@Override
public UniqueKey<UserProgramAssignmentsRecord> getPrimaryKey() {
return Keys.USER_PROGRAM_ASSIGNMENTS_PKEY;
}
/**
* {@inheritDoc}
*/
@Override
public List<UniqueKey<UserProgramAssignmentsRecord>> getKeys() {
return Arrays.<UniqueKey<UserProgramAssignmentsRecord>>asList(Keys.USER_PROGRAM_ASSIGNMENTS_PKEY);
}
/**
* {@inheritDoc}
*/
@Override
public List<ForeignKey<UserProgramAssignmentsRecord, ?>> getReferences() {
return Arrays.<ForeignKey<UserProgramAssignmentsRecord, ?>>asList(Keys.USER_PROGRAM_ASSIGNMENTS__PGUA_PG___FK, Keys.USER_PROGRAM_ASSIGNMENTS__PGUA_UI___FK);
}
/**
* {@inheritDoc}
*/
@Override
public TableField<UserProgramAssignmentsRecord, LocalDateTime> getRecordTimestamp() {
return MODIFIED_DATE;
}
/**
* {@inheritDoc}
*/
@Override
public UserProgramAssignments as(String alias) {
return new UserProgramAssignments(DSL.name(alias), this);
}
/**
* {@inheritDoc}
*/
@Override
public UserProgramAssignments as(Name alias) {
return new UserProgramAssignments(alias, this);
}
/**
* Rename this table
*/
@Override
public UserProgramAssignments rename(String name) {
return new UserProgramAssignments(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public UserProgramAssignments rename(Name name) {
return new UserProgramAssignments(name, null);
}
}
有什么想法吗?