Given
* A simple entity * A polymorphic entity
When
* querying the simple entity doing a left join with the polymorphic entity
Then
* The results will always be empty when the left joined entity is not present
{code:java}@Entity @Table(name = "ENTITY_B") public class EntityB { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private Integer id;
@Column(name = "SOME_ID") private Integer someId; }{code}
{code:java}@Entity @Table(name = "ENTITY_A") @DiscriminatorColumn(name = "DISC_COL", discriminatorType = DiscriminatorType.INTEGER) public abstract class AbstractEntityA { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id; }{code}
{code:java}@Entity @DiscriminatorValue("1") public class EntityAChildOne extends AbstractEntityA {}{code}
{code:java} @Test public void hhhXXXXTest() throws Exception { try (Session s = openSession()) { Transaction tx = s.beginTransaction(); EntityB entityB = new EntityB(); s.persist(entityB); tx.commit(); }
try (Session s = openSession()) { Query<EntityB> query = s.createQuery( "select b from EntityB b left outer join EntityAChildOne a1 on a1.id = b.someId", EntityB.class); List<EntityB> actual = query.list(); assertThat(actual).hasSize(1); } }{code}
{noformat}java.lang.AssertionError: Expected size:<1> but was:<0> in: <[]> at org.hibernate.bugs.ORMUnitTestCase.hhhXXXXTest(ORMUnitTestCase.java:76) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.lang.Thread.run(Thread.java:833){noformat}
In hibernate 5.6.15, this example just works, because hibernate 5 completely ignores the discriminator when generating the SQL (might be a limitation or a bug), but the expected outcome for the example is kept and we get the expected result which is the single list with the entity b in it:
{noformat}Hibernate 5.6.15: select entityb0_.ID as id1_1_, entityb0_.SOME_ID as some_id2_1_ from MY_TABLE entityb0_ left outer join ENTITY_A entityachi1_ on ( entityachi1_.ID=entityb0_.SOME_ID ){noformat}
In hibernate 6.2.0, the resulting list will always be empty, because hibernate 6 does not ignore the discriminator, but adds it to the main query criteria (this part is the wrong part). Since this is a left join, the joined entity is optional and adding the discriminator criteria to the main part of the query will result to empty when the joined entity is not present.
{noformat}Hibernate 6.2.0: select e1_0.ID, e1_0.SOME_ID from MY_TABLE e1_0 left join ENTITY_A e2_0 on e2_0.ID=e1_0.SOME_ID where e2_0.DISC_COL=1{noformat}
A failing test scenario is to be attached and also available here [https://github.com/ratoaq2/HHH-16438|https://github.com/ratoaq2/HHH-16438|smart-link] |
|