Support constructor expression mixed with other select expressions

Description

In Hibernate if a constructor expression is used, it must be the only select expression in the query.
So select new a.b.Foo(p.name, p.city) from Person p is supported, but these are not:

  • select p.id, new a.b.Foo(p.name, p.city) from Person p

  • select new a.b.Foo(p.name, p.city), new a.b.Bar(p.age) from Person p

  • etc

However, these should be supported according to the official JPQL syntax:
{{
select_clause ::= SELECT [DISTINCT] select_expression {, select_expression}*
select_expression ::= single_valued_path_expression | aggregate_expression | identification_variable | OBJECT(identification_variable) | constructor_expression
constructor_expression ::= NEW constructor_name ( constructor_item {, constructor_item}* )
}}
See also https://hibernate.atlassian.net/browse/HHH-3082.

I am working on a Pull Request to support this.

Environment

None

Activity

Show:
Steve Ebersole
August 26, 2015, 3:32 AM

In the existing code, this issue is not so much the query nor the parsing of it. The issue is org.hibernate.Query#getReturnTypes and the fact that constructor expressions are not expressible as Hibernate types.

Sure you could return null or some other magic value for the constructor expression for its position within the return types array, but that would be a hack like any other magic values. We could dynamically build a BasicType to represent the constructor expression.

Anyway, I will address this in the HQL redesign (see ) but that work will not be integrated into Hibernate ORM, OGM, etc for quite some time. In that meantime, if you come up with a solution (PR) we can discuss including this prior to that the SQM work being integrated.

Marcus Klimstra
August 26, 2015, 9:23 AM

, I've been working on this for a while, but until now had only committed the parser change. You can see my current WIP here: https://github.com/marcus-nl/hibernate-orm/commit/26314164f1c65792fa9359f7f5f65611306536a4 . I think it's about 80% functional (but you know what they say about the remaining 20%... ).

Steve Ebersole
September 1, 2015, 3:11 PM

- you might want to shift your focus to the work we are doing to redesign all this. If you ping me about it on IRC we can chat about it.

Steve Ebersole
December 24, 2016, 8:16 AM

This is implemented already in wip/6.0. The expanded features include:

  1. ability to select multiple dynamic-instantiations

  2. ability to nest dynamic-instantiations

  3. ability to use setter injection of the values into the dynamically instantiated bean

Steve Ebersole
December 6, 2018, 3:43 PM

Preparing Alpha1 release

Assignee

Steve Ebersole

Reporter

Marcus Klimstra

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Priority

Major
Configure