Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where IN clause - multi column (querydsl)

I have three integer values of two pairs.
I would like to use this as a list of IN in the WHERE clause.

(2019, 5) (2019, 6) (2019, 7)

I want to use the above two-pair list in a query like this:

SELECT
    *
FROM
    WEEK_REPORT A
WHERE
    A.user_id IN ('test2','test5' ) AND
    (A.year, A.week_no) IN ((2019,4),(2019,5),(2019,6));

To that end, I wrote the source as follows:

// lastYear=2019, lastWeekNo=4
Tuple t = JPAExpressions.select(Expressions.constant(lastYear), Expressions.constant(lastWeekNo))
    .from(weekReport)
    .fetchOne();

// currentYear=2019, currentWeekNo=5        
Tuple t2 = JPAExpressions.select(Expressions.constant(currentYear), Expressions.constant(currentWeekNo))
    .from(weekReport)
    .fetchOne();

// nextYear=2019, nextWeekNo=4
Tuple t3 = JPAExpressions.select(Expressions.constant(nextYear), Expressions.constant(nextWeekNo))
    .from(weekReport)
    .fetchOne();

return queryFactory
    .select(weekReport)
    .from(weekReport)
    .where(weekReport.user.eq(user)
        .and(Expressions.list(weekReport.year, weekReport.weekNo).in(t, t2, t3)))
    .fetch();

However, the correct result is not output and an error occurs.

java.lang.UnsupportedOperationException: null
    at com.querydsl.jpa.JPASubQuery.fetchOne(JPASubQuery.java:66) ~[querydsl-jpa-4.1.4.jar:na]

I looked it up in the official document but it does not come out.
Is there a way?

Thank you.

like image 594
unilep Avatar asked Jan 31 '19 06:01

unilep


1 Answers

how about to pass a list of tuples as one parameter

List<Tuple> params = ...
params = [(2019,4),(2019,5),(2019,6)]

return queryFactory
    .select(weekReport)
    .from(weekReport)
    .where(weekReport.user.eq(user)
        .and(Expressions.list(weekReport.year, weekReport.weekNo).in(params)))
    .fetch();
like image 160
eshirvana Avatar answered Oct 05 '22 23:10

eshirvana