【发布时间】:2022-10-17 16:46:17
【问题描述】:
我有一个带有创建时间戳 (timestamp) 和生存时间 (interval) 的表。
CREATE TABLE my_object (
id uuid NOT NULL PRIMARY KEY,
created timestamp NOT NULL,
time_to_live interval NOT NULL
);
现在我想找到所有对象,它们的 TTL 结束了。我试过这样的事情:
public class MyObjectRepository {
public Stream<MyObjectDto> fetchExpired() {
return context
.selectFrom(MY_OBJECT)
.where(localDateTimeDiff(currentLocalDateTime(), MY_OBJECT.CREATED)
.greaterThan(MY_OBJECT.TIME_TO_LIVE))
// ^- compile-error, no overload accepts TableField<MyObjectRecord, Duration>
.forUpdate()
.skipLocked()
.fetchStreamInto(MyObjectDto.class);
}
}
也许这里的大问题是,我将 TTL 强制输入java.time.Duration。但对于干净的 API,我无法将类型更改为 DayToSecond。
<!-- others -->
<forcedType>
<userType>java.time.Duration</userType>
<converter>org.jooq.Converter.ofNullable(
org.jooq.types.YearToSecond.class, Duration.class,
yearToSecond -> yearToSecond.toDuration(), duration -> org.jooq.types.YearToSecond.valueOf(duration)
)
</converter>
<includeTypes>INTERVAL</includeTypes>
</forcedType>
<!-- others -->
我怎样才能在 JOOQ 中做到这一点?
【问题讨论】: