Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Column In clause Parameter #2910

Closed
LukeChow opened this issue Jun 16, 2021 · 2 comments
Closed

Multiple Column In clause Parameter #2910

LukeChow opened this issue Jun 16, 2021 · 2 comments
Labels

Comments

@LukeChow
Copy link

@Entity
@Data
class Image {
            private long id;
            private long imgId;
            private int index;
}
//In service, select all that its index and imgId in a map
HashMap<Integer, Long> usedImageMap = ...
List<Tuple> list = new ArrayList<>();
//But how to initial list from usedImageMap???
queryFactory.selectFrom(qImage)
                .where(Expressions.list(qImage.index, qImage.imgId).in(list))
                ....

Maybe this is a solution, but it is ugly!

usedImageMap.forEach((index, imgId) -> {
            list.add(new Tuple() {
                @Nullable
                @Override
                public <T> T get(int i, Class<T> type) {
                    if (i == 0) return (T)index;
                    return (T)imgId;
                }

                @Nullable
                @Override
                public <T> T get(Expression<T> expr) {
                    if (expr.equals(qImage.index)) return (T)index;
                    return (T)imgId;
                }

                @Override
                public int size() {
                    return 2;
                }

                @Override
                public Object[] toArray() {
                    return Arrays.asList(index, imgId).toArray();
                }
            });
        });
@jwgmeligmeyling
Copy link
Member

jwgmeligmeyling commented Jun 16, 2021

Well what happens if qImage.index is not unique? Allow Multimap as input variable as well as a Map? Then what if the input index is actually inversed? How to handle tuples greater than 2, like Expressions.list(a, b, c).in(...)? IMO these decisions will always be use case specific so its not particularly sensible to build something in QueryDSL for this (I am open to suggestions though).

Meanwhile: what about:

Expressions.list(qImage.index, qImage.imgId).in(usedImageMap.entrySet().stream()
                .map(entry -> Expressions.list(Expressions.constant(entry.getKey()), Expressions.constant(entry.getValue())))
                .toArray(Expression[]::new))

@LukeChow
Copy link
Author

Well what happens if qImage.index is not unique? Allow Multimap as input variable as well as a Map? Then what if the input index is actually inversed? How to handle tuples greater than 2, like Expressions.list(a, b, c).in(...)? IMO these decisions will always be use case specific so its not particularly sensible to build something in QueryDSL for this (I am open to suggestions though).

Meanwhile: what about:

Expressions.list(qImage.index, qImage.imgId).in(usedImageMap.entrySet().stream()
                .map(entry -> Expressions.list(Expressions.constant(entry.getKey()), Expressions.constant(entry.getValue())))
                .toArray(Expression[]::new))

Thank you for your reply.

@querydsl querydsl locked and limited conversation to collaborators Jun 23, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants