延迟获取不适用于 EclipseLink 中的 OneToOne 关系
Lazy fetch not work for OneToOne relation in EclipseLink
在我的项目中,我使用 EclipseLink 作为 JPA 实现。我有两个实体,Product 和 ProductDetail:
- 它们是一对一的关系。一个产品应该只有一个 ProductDetail。
- ProductDetail 不能为空。一个产品应始终具有 ProductDetail。
- 关系是单向的。我只会从 Product 访问 ProductDetail。
- 两个实体应该共享主键,Product 的 "id" 应该等于 ProductDetail 的 "prodId"。
所以我设计了如下实体模型:
@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....
}
但是懒惰的抓取从来没有奏效。 ProductDetail 始终与产品一起获取。我检查了很多文件,但仍然无法弄清楚。有没有人有这方面的经验?非常感谢!
注意:我使用的是 EclipseLink 但不是 Hibernate。
要在 toOne 关系上进行延迟加载,EclipseLink 必须将代理注入到引用中。这个过程叫做 "weaving".
默认情况下未启用此功能,因此您必须查看有关如何为运行时环境启用编织的文档:
https://www.eclipse.org/eclipselink/documentation/2.7/solutions/testingjpa004.htm#CHDEECDB
在我的项目中,我使用 EclipseLink 作为 JPA 实现。我有两个实体,Product 和 ProductDetail:
- 它们是一对一的关系。一个产品应该只有一个 ProductDetail。
- ProductDetail 不能为空。一个产品应始终具有 ProductDetail。
- 关系是单向的。我只会从 Product 访问 ProductDetail。
- 两个实体应该共享主键,Product 的 "id" 应该等于 ProductDetail 的 "prodId"。
所以我设计了如下实体模型:
@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....
}
但是懒惰的抓取从来没有奏效。 ProductDetail 始终与产品一起获取。我检查了很多文件,但仍然无法弄清楚。有没有人有这方面的经验?非常感谢!
注意:我使用的是 EclipseLink 但不是 Hibernate。
要在 toOne 关系上进行延迟加载,EclipseLink 必须将代理注入到引用中。这个过程叫做 "weaving".
默认情况下未启用此功能,因此您必须查看有关如何为运行时环境启用编织的文档:
https://www.eclipse.org/eclipselink/documentation/2.7/solutions/testingjpa004.htm#CHDEECDB