[英]How to Set null Into QueryDSL Timestamp Column During Update?
I have the following table in Postgre: 我在Postgre中有下表:
CREATE TABLE user_attempts
(
id INT PRIMARY KEY NOT NULL,
username VARCHAR(50) NOT NULL,
attempts SMALLINT NOT NULL,
lastmodified TIMESTAMP,
FOREIGN KEY ( username ) REFERENCES users ( username )
);
I would like to update the lastmodified
field through QueryDSL and Spring Data JDBC Extension as follow: 我想通过QueryDSL和Spring Data JDBC Extension更新
lastmodified
字段,如下所示:
template.update(userAttempts, update -> update
.set(userAttempts.attempts, (short) 0)
.set(userAttempts.lastmodified, null) // compilation error here
.where(userAttempts.username.eq(username))
.execute();
However, it seems that QueryDSL's set method can't accept null
because it will match more than one function signatures. 但是,似乎QueryDSL的set方法不能接受
null
因为它将匹配多个函数签名。
Based on this QueryDSL issue: https://github.com/querydsl/querydsl/issues/846 基于此QueryDSL问题: https : //github.com/querydsl/querydsl/issues/846
I should use setNull
method instead: 我应该使用
setNull
方法:
template.update(userAttempts, update -> update
.set(userAttempts.attempts, (short) 0)
.setNull(userAttempts.lastmodified)
.where(userAttempts.username.eq(username))
.execute()
);
You'll probably have to cast null
to the type of lastmodified
. 您可能必须将
null
转换为lastmodified
的类型。 Eg: 例如:
.set(userAttempts.lastmodified, (Timestamp) null)
The reason for this is that the null
literal is ambiguous when calling overloaded generic methods. 原因是在调用重载的泛型方法时,
null
文字是不明确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.