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
@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;
}
@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;
}
@Entity
@DiscriminatorValue("1")
public class EntityAChildOne extends AbstractEntityA {}
@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);
}
}
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:
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.
A failing test scenario is to be attached |