Put enum constant value in query result

382 views
Skip to first unread message

Paweł K.

unread,
Apr 21, 2015, 7:33:41 AM4/21/15
to quer...@googlegroups.com
Hello,

I have a use case in which I need to put enum constant in query result. My query looks like this:

QTable qTable = QTable.table;

SomeDTO someDTO = query.singleResult(Projections.fields(SomeDTO.class,
                ...,
                someBooleanExpression ? qTable.enumField : <ENUM_CONSTANT>,
                ...));

I am looking for a way to put some constant enum value in the place of ENUM_CONSTANT.

I tried Expresssions.as, Expressions.constantAs or EnumTemplate in various combinations, but none of them worked. I think there must be a simple way to fix this, but I just don't know how.

I'm looking for something like:

(MyEnum.MyValue).as("enumField")

Any help appreciated :)

Ruben Dijkstra

unread,
Apr 21, 2015, 7:36:56 AM4/21/15
to quer...@googlegroups.com, Paweł K. via Querydsl
Hello,

The way to serialize constants would be using 'Expressions.constant(MyEnum.MyValue)'.

Best regards,

Ruben
--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Paweł K.

unread,
Apr 21, 2015, 8:30:41 AM4/21/15
to quer...@googlegroups.com
Unfortunately I need some alias for this value and simple Expressions.constant is not working. Expressions.constant doesn't have 'as' method.

Ruben Dijkstra

unread,
Apr 21, 2015, 8:35:40 AM4/21/15
to quer...@googlegroups.com, Paweł K. via Querydsl
Hello,

I just now noticed you already tried constantAs().

Maybe if you give a little more details as to how the query looks like, we could maybe give you some directions.

Have you looked into a case expression?

Best regards,

Ruben
--

Paweł K.

unread,
Apr 21, 2015, 8:40:00 AM4/21/15
to quer...@googlegroups.com
It results with an error: Unsupported expression MyValue used as you suggested.

Paweł K.

unread,
Apr 21, 2015, 8:49:35 AM4/21/15
to quer...@googlegroups.com
Unfortunately I cannot use case expressions as someBooleanExpression is simple boolean and CaseBuilder expects BooleanExpression.

I didn't provide more details as I thought adding an enum constant to the query result wouldn't be that hard and information provided will be enough to solve my problem.

Thank you for fast response :).

Ruben Dijkstra

unread,
Apr 21, 2015, 9:00:07 AM4/21/15
to quer...@googlegroups.com, Paweł K. via Querydsl
Hello,

Expressions.constantAs should work. Could you provide details about why it doesn't work and what exactly didn't work?

If all else fails you can create a ticket on GitHub with a use case we can reproduce so we can look into this closer.

Best regards,

Ruben
--

Paweł K.

unread,
Apr 21, 2015, 9:11:44 AM4/21/15
to quer...@googlegroups.com
I tried something like:

     Expressions.constantAs(MyEnum.MyValue, new EnumPath<MyEnum>(MyEnum.class, "myFieldAlias"))

and I got an error:

     org.hibernate.QueryException: Parameters are only supported in SELECT clauses when used as part of a INSERT INTO DML statement

Is my statement above wrong?


W dniu wtorek, 21 kwietnia 2015 15:00:07 UTC+2 użytkownik Ruben Dijkstra napisał:
Hello,

Expressions.constantAs should work. Could you provide details about why it doesn't work and what exactly didn't work?

If all else fails you can create a ticket on GitHub with a use case we can reproduce so we can look into this closer.

Best regards,

Ruben

Reply all
Reply to author
Forward
0 new messages