We're updating the issue view to help you get more done. 

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:

1 2 3 4 5 6 @Entity class Person { @Id Long id; ... }

and given a query like:

1 select new DTO( p ) from Person p

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

1 2 3 4 class DTO { public DTO(Long id) {...} ... }

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.

Environment

None

Status

Assignee

Unassigned

Reporter

Steve Ebersole

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Priority

Major