in my project I used EclipseLink as the JPA implementation. And I have two entities, Product and ProductDetail:
So I designed the entity models like following:
@Table(name="product")
public class Product{
@Id
@GeneratedValue(generator = "PRODUCT_ID")
@UuidGenerator(name = "PRODUCT_ID")
@Column(name = "id", unique=true, nullable=false, length=200)
private String id;
// Some other properties....
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@PrimaryKeyJoinColumn
private ProductDetail productDetail;
}
@Table(name="product_detail")
public class ProductDetail{
@Id
@Column(name = "prod_id", unique=true, nullable=false, length=200)
private String prodId;
// Some other properties....
}
But the lazy fetching never worked. The ProductDetail is always fetched with the Product. I checked many documents but still cannot figure it out. Does anyone have some experience on this? Thanks a lot!
NOTICE: I'm using EclipseLink but not Hibernate.
To make lazy loading on toOne relationships work EclipseLink must inject a proxy into the reference. This process is called "weaving".
This is not enabled by default so you have to check out the documentation how to enable weaving for your runtime environment:
https://www.eclipse.org/eclipselink/documentation/2.7/solutions/testingjpa004.htm#CHDEECDB