java - 避免在 Eclipselink 中实例化 IndirectList

标签 java jpa eclipselink

我在父项和子项之间有一个简单的一对多关系。

父级:

@OneToMany(mappedBy = "parent", orphanRemoval = true, cascade = CascadeType.ALL)
private List<Child> children = new ArrayList<>();

child :

@ManyToOne(optional = false)
@JoinColumn(name = "PARENT_ID", nullable = false)
private Parent parent;

因为 parent 可以有大量的 child ,所以我想利用 Lazy Instantiation of Indirect Collections :

IndirectList and IndirectSet can be configured not to instantiate the list from the database when you add and remove from them. IndirectList defaults to this behavior. When Set to true, the collection associated with this TransparentIndirection will be setup so as not to instantiate for adds and removes. The weakness of this setting for an IndirectSet is that when the set is not instantiated, if a duplicate element is added, it will not be detected until commit time.

由于 OneToMany 的默认 FetchType 是 LAZY,并且我使用 List 作为我的 Collection,因此从数据库加载父级会导致 IndirectList 用于关系。一旦我向该父级添加另一个子级,我就可以看到执行该父级的子级的选择查询。

我该如何改变它? 我正在使用 Eclipselink 2.6.4 ( org.eclipse.persistence:eclipselink:2.6.4 )。

我还尝试使用 DescriptorCustomizer 来调用 org.eclipse.persistence.mappings.CollectionMapping.setUseLazyInstantiationForIndirectCollection(Boolean)对我的关系,但这似乎完全没有影响。

调试到方法 org.eclipse.persistence.indirection.IndirectList.add(E) 后,我能够看到方法调用 org.eclipse.persistence.indirection.IndirectList.shouldAvoidInstantiation()在第 206 行返回 false,因为 org.eclipse.persistence.indirection.IndirectList._persistence_getPropertyChangeListener()第 1007 行返回 null,并且 null 不是 AttributeChangeListener 的实例。因此,该关系随后由 org.eclipse.persistence.indirection.IndirectList.getDelegate() 实例化。在第 216 行。

对我来说,这似乎是一个错误,但我对此实现的了解还不够确定。

最佳答案

需要更改跟踪来支持在进行修改时不实例化惰性集合。使用编织时会启用更改跟踪,如下所述: https://www.eclipse.org/eclipselink/documentation/2.5/concepts/app_dev007.htm

关于java - 避免在 Eclipselink 中实例化 IndirectList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407628/

相关文章:

java - Android 计时器更新 TextView (UI)

java - 得到这个 org.springframework.web.servlet.DispatcherServlet noHandlerFound 错误和警告 : No mapping found for HTTP request URI in Spring MVC

java - JPA规范: filter child entities

MATLAB 仅在最初几秒内找到 eclipselink 的 persistence.xml

java - 执行 native 更新查询后,检索到的实体中仍然存在旧值

java - 如何在我的 JAR 中嵌入外部 .wav 文件?

java - 带有spring mvc的ajax总是出错

java - Spring JPA @IdClass CompositeKey 最低要求

java - 使用 JPA Criteria API 生成正确和/或条件

java - JPA2 : Nested Primary Key class cannot persist as binding null