Reputation: 53
I have a simple Hibernate @ManyToOne
mapping between two entities, using an association table thanks to annotation @JoinTable
.
Here is my mapping:
@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;
}
}
and
@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;
}
}
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: 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
.
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. Is there any way to prevent Hibernate from generating the left join to customer_address_association
table? It impacts the performance on large tables.
Upvotes: 2
Views: 1320