Allow dynamic-instantiation queries to use ctors accepting entities rather than just ids

Description

Historically (well before JPA) HIbernate would handle dynamic instantiation queries in cases where one of the arguments is an entity-reference by passing just the entity's identifier rather than a complete reference to the entity. To be clear, I am talking about a query like:

select new DTO( p ) from Person p

Hibernate implicitly treats this like:

select new DTO( p.id ) from Person p

and expects DTO to have a ctor taking the appropriate ID type.

E.g., assuming a Person:

and given a query like:

Hibernate would expect DTO to be defined as (specifically the ctor):

DTO could also define a ctor taking Person as the arg, Hibernate would just not never use that form.

Along comes JPA and also defines support for dynamic instantiation queries, but does not specify one way or the other how this case should be handled. I have been told other providers interpret this the opposite way. Makes sense. I think it is time we at least allow that as an option. Or maybe a nicer implementation that looks for both and picks the available one (if that's not too much effort).

The proposal is proposing was for adding some support for this in 6.0 based on the SQM work where we can either:

  1. add a flag that says whether to support legacy behavior, or this new behavior

  2. attempt to dynamically infer what to do based on available ctors

(2) would be awesome I think. We'd still have to know how to handle cases where the DTO defined ctors matching both cases and which to prefer.

Activity

Show:

Andrea Boriero February 17, 2017 at 2:35 PM

I agree (2) would be great with a config option to force the legacy behaviour.

Steve Ebersole February 17, 2017 at 1:24 PM

Scheduled for Alpha1, but this is something I'd expect we tackle in a alter Alpha (which are just not set up in Jira as of yet)

Details

Assignee

Reporter

Components

Fix versions

Priority

Created February 17, 2017 at 1:23 PM
Updated June 24, 2022 at 1:56 PM