【发布时间】:2016-02-13 12:37:16
【问题描述】:
我有一个由 Jooq 生成的 PersonPojo 和 PersonRecord。
现在我想做这样的事情:
Person p = new PersonPojo()
p.setId(10);
create.selectFrom(Tables.PERSON).whereLike(p).fetch();
当前版本(3.7)可以吗?
【问题讨论】:
我有一个由 Jooq 生成的 PersonPojo 和 PersonRecord。
现在我想做这样的事情:
Person p = new PersonPojo()
p.setId(10);
create.selectFrom(Tables.PERSON).whereLike(p).fetch();
当前版本(3.7)可以吗?
【问题讨论】:
Query By Example (QBE) 支持在 jOOQ 3.8 中通过#4735 实现。你可以写:
Person p = new PersonPojo();
p.setId(10);
PersonRecord record = new PersonRecord();
record.from(p); // Reuse pre-existing reflection functionality here.
create.selectFrom(Tables.PERSON).where(DSL.condition(record)).fetch();
更多详情请参考Javadoc:
在较旧的 jOOQ 版本中,您可以自己实现 QBE:
Person p = new PersonPojo();
p.setId(10);
PersonRecord record = new PersonRecord();
record.from(p); // Reuse pre-existing reflection functionality here.
Condition condition = DSL.trueCondition();
for (Field<?> field : record.fields())
if (record.getValue(field) != null)
condition = condition.and(((Field) field).eq(record.getValue(field)));
create.selectFrom(Tables.PERSON).where(condition).fetch();
create.selectFrom(Tables.PERSON)
.where(Stream.of(record.fields())
.filter(f -> record.getValue(f) != null)
.reduce(
DSL.trueCondition(),
(c, f) -> c.and(((Field) f).eq(record.getValue(f))),
(c1, c2) -> c1.and(c2)
))
.fetch();
【讨论】:
DSL.trueCondition(); 方法。