[hibernate-dev] Add entity with InheritanceType.JOINED to native query

Yoann Rodiere yrodiere at redhat.com
Mon Sep 12 07:19:25 EDT 2016


Hi,

I'll let it to actual Hibernate ORM developers (which I'm not) to give you
a proper answer (if they have one), but this seems like a bug to me. You
best course of action may be to provide a test case and provide it as part
of a ticket on JIRA: https://hibernate.atlassian.net/ .

If you really are in a hurry and can't wait for this to be fixed...
My previous team had to work this around in one of our projects once, and
we ultimately resorted to some ugly hack that would rely on the fact that
Hibernate generates semi-deterministic aliases for JOINed tables. I don't
remember the specifics, but this definitely involved guessing
Hibernate-generated aliases.
For instance, if "s" is your main table alias, Hibernate will use something
like "s_1_" for the table mapped to the first child class, "s_2_" for the
second child class, and so on. You can then try guessing the aliases and
make use of them in your JOIN clause instead of your custom alias ("{a}"),
being careful to remove the braces. The order seems to be stable for each
subsequent execution, but has been seen to change when doing major code
updates (though I couldn't say which).
As I said, this is ugly. It may do for non-essential, well-tested code that
won't have to evolve much, but this isn't necessarily your case.

Yoann

On 7 September 2016 at 03:41, Jan-Willem Gmelig Meyling <
jan-willem at youngmediaexperts.nl> wrote:

> Hi Hibernate developers,
>
> I stumbled upon an issue today and I am wondering whether the following
> would be possible using Hibernate, and if not, whether such could actually
> be implemented, or if I actually hit a boundary of the alias injection.
>
> My problem has to do with trying to map the result set of a native query
> to an entity that uses the JOINED InheritanceType. After adding the enitity
> to my NativeQuery as follows, the aliases for the tables of the subclasses
> are not set properly (obviously). My question is, though, would it be
> possible to set these as well? As I can’t figure this out from the current
> documentation (that only states that all columns should be present in the
> result set, which I assume to apply to a InheritanceType.SINGLE_TABLE
> primarily).
>
> The code I am currently using to construct the query is as follows:
>
>
> session
>    .createNativeQuery("SELECT {s.*} FROM my_super {s} LEFT JOIN my_sub_a
> {a} USING (id)")
>    .addEntity("s", MySuper.class)
>    .getSingleResult();
>
> Additional code is available on Github gist: https://gist.github.com/
> JWGmeligMeyling/51e8a305f3c268eda473511e202f76e8
>
> And my question is also posted on Stackoverflow (to gain some extra
> credits for the answer ;-) ):
> http://stackoverflow.com/questions/39359924/add-entity-
> with-inheritancetype-joined-to-native-query
>
> Thanks in advance,
>
> Jan-Willem
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev


More information about the hibernate-dev mailing list