entity-join ignores discriminator value

Description

entity-join (join com.acme.User) ignores discriminator value for polymorphous entities whereas an implicit join path (join p.address.city) filters discriminator values

Given the following entity mapping:

@Entity
@Table(name = "Animal")
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = CHAR)
@DiscriminatorValue("A")
public class Animal {

@Id
@GeneratedValue
private Long id;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Zoo zoo;

@Entity
@DiscriminatorValue("D")
public class Dog extends Animal {
}

@Entity
@DiscriminatorValue("C")
public class Cat extends Animal {
}

@Entity
public class Zoo {
@Id
@GeneratedValue
private Long id;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "zoo", targetEntity = Cat.class, fetch = FetchType.LAZY, orphanRemoval = true)
private final List<Cat> cats = new ArrayList<>();

@OneToMany(cascade = CascadeType.ALL, mappedBy = "zoo", targetEntity = Dog.class, fetch = FetchType.LAZY, orphanRemoval = true)
private final List<Dog> dogs = new ArrayList<>();
...
}

I would expect the following queries to return count 1. Query 2 incorrectly returns count 2.

Query 1:
select count(zoo)
from Zoo zoo
inner join zoo.dogs as dog

Query 2:
select count(zoo)
from Zoo zoo
inner join Dog dog with dog.zoo = zoo

We reproduced this behaviour with versions 5.3.20.Final and 5.4.30.Final.

The attached project demonstrates this behaviour (de.playground.DiscriminatorValueJoinBehaviourTest

).

Environment

None

Assignee

Unassigned

Reporter

Sebastian Hinzelmann