简体   繁体   中英

Lazy fetch not work for OneToOne relation in EclipseLink

in my project I used EclipseLink as the JPA implementation. And I have two entities, Product and ProductDetail:

  • They have one-to-one relation. One Product should have only one ProductDetail.
  • ProductDetail could not be null. One Product should always has ProductDetail.
  • The relation is unidirection. I will only access ProductDetail from Product.
  • The two entities should have shared primary key, the "id" of a Product should be equal to "prodId" of the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM