[JIRA] (HHH-16885) Hibernate 6.x changes outer to inner join when @EntityGraph and @Id
by Konrad Wajs (JIRA)
Konrad Wajs ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5dea973... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiOGZlMTYzZDUy... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16885?atlOrigin=eyJpIjoiOGZlMT... ) HHH-16885 ( https://hibernate.atlassian.net/browse/HHH-16885?atlOrigin=eyJpIjoiOGZlMT... ) Hibernate 6.x changes outer to inner join when @EntityGraph and @Id ( https://hibernate.atlassian.net/browse/HHH-16885?atlOrigin=eyJpIjoiOGZlMT... )
Issue Type: Bug Affects Versions: 6.2.6 Assignee: Unassigned Components: hibernate-core Created: 03/Jul/2023 12:50 PM Environment: spring boot 3.1.1
Java 17
Postgres and H2 Priority: Critical Reporter: Konrad Wajs ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5dea973... )
I'm using @EntityGraph to eagerly load some attributes.
However, something broke after updating from 5.x to 6.x.
Suppose we have a human who can have multiple houses:
public class Human {
@Id
@Column(name = "ID" , nullable = false , updatable = false , precision = 20)
@GeneratedValue
private BigInteger id;
@EqualsAndHashCode.Exclude
@ToString.Exclude
@OneToMany(mappedBy = "human" , cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
private Collection<House> houses;
}
When searching for a human, by ID, EntityGraph can eagerly load some attributes:
@EntityGraph(attributePaths = { "houses.address" })
@Query ( "SELECT h FROM Human h WHERE h.id = ?1" )
Human findByIdEagerHouseAddresses( Integer id);
This will create SQL with left (outer by default) joins:
select h1_0.id, h2_0.human_fk, h2_0.address_fk, a1_0.id
from human h1_0
left join house h2_0 on h1_0.id = h2_0.human_fk
left join address a1_0 on a1_0.id = h2_0.address_fk where h1_0.id in (?)
This is acceptable because I will get the human even if they don't have a house.
However, if the address field in the house has the @Id annotation, the outer join will change to "inner":
@Entity
public class House {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "human_fk" , nullable = false , updatable = false )
private Human human;
@Id // THIS ANNOTATION WAS ADDED
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "address_fk" , nullable = false , updatable = false )
private Address address;
}
select h1_0.id, h2_0.human_fk, h2_0.address_fk, a1_0.id
from human h1_0
left join house h2_0 on h1_0.id = h2_0.human_fk
join address a1_0 on a1_0.id = h2_0.address_fk where h1_0.id in (?)
If a human does not have a house, the query returns nothing due to the inner join, which seems like a bug.
( https://hibernate.atlassian.net/browse/HHH-16885#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16885#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100228- sha1:e911e2c )
2 years, 5 months