1

I a trying to write below query in querydsl

SELECT u.id
FROM users AS u
   JOIN unnest(ARRAY[2,2,1]) WITH ORDINALITY AS arr(elem, ord)
      ON u.id = arr.elem
ORDER BY arr.ord;

How can I join on array if ids

I tried registering unnest function too

public class Contributor implements MetadataBuilderContributor {
    @Override
    public void contribute(MetadataBuilder metadataBuilder) {
        metadataBuilder.applySqlFunction("unnest",
                new SQLFunctionTemplate(StandardBasicTypes.STRING, "array[?1]"));
    }
}

But this is also not working, I am getting unexpected token: unnest

1 Answer 1

0

Well, the problem is that while PostgreSQL has support for native arrays and the unnest operator, JPA currently does not in its query language (JPQL) which Querydsl / Spring Boot have to use as an intermediary. So unfortunately, this is not supported.

Sign up to request clarification or add additional context in comments.

3 Comments

I saw one can register functions eg. stackoverflow.com/questions/70584908/… or stackoverflow.com/questions/54686971/… but this is not working for me.
Yes, because even though you can register custom functions, it's not possible to use functions as join target or from clause.
ah I see. Thanks for this info. Wondering if it is possible to write native query for above using querydsl but use its dynamic projection to select columns at runtime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.