AssertionError when using using native query on table with InheritanceStrategy.JOINED
Description
Activity

Jon Harper September 25, 2023 at 9:45 AM
Hi Jan,
thank you for your time and your answer. I can confirm that add “null as companyName” makes the test pass. Also thanks for the link (small typo there’s an extra comma at the end of your link). Since having non empty class hierarchies is the only real world use case, maybe the test JoinedSubclassNativeQueryTest can be modified (or duplicated and modified) to reflect this real world use case ?
I’m not sure what is supposed to happen when you use native queries and upcast the resulting entity (for example forcing 0 for all entities since they all inherit the base), is it a supported use case ? Should it be documented ? Does hibernate try to delete data from the child class properties since it now sees the entity with it’s id as a base class instance ?
For example, should this note: elaborate more about it, or provide a clickable link to a section that elaborates on these questions ?
Cheers,
jon

Jan Schatteman September 18, 2023 at 4:50 PM
Hello,
Sorry for the delay.
You are issuing a polymorphic query, therefore, according to you need to include all properties for the base class and all its subclasses.
Using polymorphism with native queries doesn't actually make a lot of sense, and since you clearly don't want to recover data from the other tables you could just do something like
We do acknowledge that this behaviour is different from Hibernate 5, because with respect to Hibernate 6 there has been a change in the implementation of this. However it was not documented (even in 5) to work the way you indicated, so we cannot consider this to be a bug (unless you prove me wrong w/ respect to this not being documented that way in 5 ).

Jon Harper September 11, 2023 at 9:05 PM
Hi,
I think there’s still a bug when the class hierarchy is not empty ? Please see ?

Christian Beikov March 3, 2023 at 8:42 AM
Note that your test case is kind of broken, as you rely on fetching the entity from the first level cache i.e. persistence context and it would fail if the entity were not contained there. Try clearing the entity manager after flushing to see the test case fail even on 5.
We will clarify the requirements for native queries that want to materialize entities as part of
While upgrading to SpringBoot 3.0.2 (Hibernate 6)I encountered issue where a native query I use throws a cryptic AssertionError exception. Eventually I pinpointed the issue to be related to entity having InheritanceStrategy.JOINED and was able to reproduce in Hibernate ORM unit test.
The query in my project was specific to postgres feature but even simplest query
throws:
Reproduction test case
I was able to ascertain that:
same native query works in Hibernate 5 (attached in reproduction repo as well)
the issue occurs in Native query but not in HQL query
the database returns the result so the issue must be somewhere in the result handling
If I am doing something wrong it would be helpful if the error was able to put me on the right track, but I am not able to make heads or tails from the exception thrown.