I have a simple Hibernate @ManyToOne mapping between two entities, using an association table thanks to annotation @JoinTable. Here is my mapping: {code} @Entity public class Customer {
private Long id; private Address address;
@Id @GeneratedValue public Long getId() { return id; }
@ManyToOne @JoinTable( name = "customer_address_association", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "address_id") ) public Address getAddress() { return address; }
public void setId(Long id) { this.id = id; }
public void setAddress(Address address) { this.address = address; } } {code}
and
{code} @Entity public class Address {
private Long id; private String street;
@Id @GeneratedValue public Long getId() { return id; }
public String getStreet() { return street; }
public void setId(Long id) { this.id = id; }
public void setStreet(String street) { this.street = street; } } {code}
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: {code} select customer0_.id as col_0_0_ from customer customer0_ left outer join customer_address_association customer0_1_ on customer0_.id=customer0_1_.customer_id {code}
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. |
|