We have a one to one relationship between two tables - VIN and VIN_EXTENSION over a composite key columns VIN and CUSTOMER & EX_VIN and EX_CUSTOMER respectively.
Below is the code snippet of the relationship.
Code:
@Entity
@Table(name = "VIN")
public class Vin implements Serializable {
@EmbeddedId
private VinKey vinKey;
}
Code:
@Embeddable
public class VinKey implements Serializable {
@OneToOne(targetEntity = VinExtension.class, fetch = FetchType.LAZY)
@JoinColumns(value = {
@JoinColumn(name = "CUSTOMER", referencedColumnName = "EX_CUSTOMER"),
@JoinColumn(name = "VIN", referencedColumnName = "EX_VIN") })
private VinExtension vinExtension;
}
Code:
@Entity
@Table(name = "VIN_EXTENSION")
public class VinExtension implements Serializable {
@EmbeddedId
private VinExtensionKey vinExtensionKey;
}
Code:
@Embeddable
public class VinExtensionKey implements Serializable {
@Column(name = "EX_CUSTOMER")
private String customer;
@Column(name = "EX_VIN")
private String vin;
}
When hibernate fetches the data for Vin entity, it runs two different set of queries.
One fetches from VIN and series of queries to VIN_EXTENSION.
SELECT * FROM VIN
SELECT * FROM VIN_EXTENSION WHERE EX_CUSTOMER = ? AND EX_VIN = ?
SELECT * FROM VIN_EXTENSION WHERE EX_CUSTOMER = ? AND EX_VIN = ?
SELECT * FROM VIN_EXTENSION WHERE EX_CUSTOMER = ? AND EX_VIN = ?
..
I want hibernate to fetch the data using a normal join between VIN and VIN_EXTENSION.
Am i missing anything in the mapping?
Even though i have specified a lazy fetch type, the data from VIN_EXTENSION is always fetched.