Extra LEFT JOIN generated with @ManyToOne and @JoinTable when projecting on main entity id

Description

I have a simple Hibernate @ManyToOne mapping between two entities, using an association table thanks to annotation @JoinTable. Here is my mapping:

and

When I query on Customer entity, I always get an extra left join to the join table. For example, a HQL query such as SELECT c.id from Customer c generates the following SQL query:

I understand the left join is required when the full Customer entity is loaded, to know whether or not Address is null, but the LEFT JOIN seems totally useless in case of a projection, when just the id is selected in my example. This can be a performance issues for large tables.

Full source code to reproduce is available here: https://github.com/ndionisi/hibernate-extra-left-join

I reproduce it with Hibernate 5.1, 5.2 and 5.3.

Environment

None

Assignee

Jan-Willem Gmelig Meyling

Reporter

Nelson Dionisi

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure