How can I write WHERE
clause which is something like WHERE ? BETWEEN col1 AND col2
with QueryDSL? I know I can write as follows instead,
...
.where(mytable.col1.loe(constant), mytable.col2.goe(constant))
...
But using BETWEEN
makes the query more readable.
I use QueryDSL 3.2.3.
UPDATE:
I ended up with something like this (A testcase which runnable via mvn test
):
final int constant = 10;
final QMyTable m = QMyTable.myTable;
final SimpleExpression<Boolean> operation = Expressions.operation(Boolean.class, Ops.BETWEEN,
Expressions.constant(constant), m.col1, m.col2);
// This yields:
// SELECT ID, COL1, COL2 FROM MYTABLE WHERE ((? BETWEEN COL1 AND COL2) = ?)
// bind => [10, true]
final MyTable actual = new JPAQuery(em).from(m).where(operation.eq(true)).uniqueResult(m);
It works anyway but the part = ?
is redundant. I want more simple one something like:
SELECT ID, COL1, COL2 FROM MYTABLE WHERE (? BETWEEN COL1 AND COL2)
I have no idea how to write it. Is there any way to construct this query via QueryDSL?
I've just found this:
final int constant = 10;
final QMyTable m = QMyTable.myTable;
final BooleanExpression operation = Expressions.booleanOperation(Ops.BETWEEN,
Expressions.constant(constant), m.col1, m.col2);
// This yields:
// SELECT ID, COL1, COL2 FROM MYTABLE WHERE (? BETWEEN COL1 AND COL2)
// bind => [10]
final MyTable actual = new JPAQuery(em).from(m).where(operation).uniqueResult(m);
This constructs one which I was exactly looking for.