首页 > 解决方案 > 限制在 JPA Eclipse 中获取的子记录link

问题描述

我有一个包含子实体列表的父实体。有一些新的子实体被创建并设置为父实体。代码片段如下所示。

public class ParentEntity {
@OneToMany(fetch=FetchType.LAZY, mappedBy="parentField", cascade=CascadeType.ALL)
   private List<ChildEntityA> childAList = new ArrayList<>();
}

public class BusinessLogic {
 public void methodA() {
   ParentEntity parent = parentRepository.getById();
   parent.setFieldA("fieldA");
   ChildEntityA child1 = new ChildEntityA();
   child1.setField1("field1");
   ChildEntityA child2 = new ChildEntityA();
   child2.setField1("field1");
   List<ChildEntityA> childAList = new ArrayList<>();
   childAList.add(child1);
   childAList.add(child2);
   parent.setChildAList(childAList);//This triggers a select call on DB table associated with Child A and fetches all the records
   entityManager.merge(parent);
}
}

我的问题是 parent.setChildAList(childAList); 触发对子数据库表的 SELECT 查询,并获取数百万条不需要的记录。有没有办法限制被检索的子记录的数量或一起阻止?全选导致 GC 抖动并影响性能。

JPA 是规范,Eclipselink 是我的提供者。对此的任何帮助将不胜感激。提前致谢。

标签: javajpaeclipselink

解决方案


推荐阅读