避免在 Eclipse 中实例化 IndirectListlink
Avoid Instantiation of IndirectList in Eclipselink
我在 Parent 和 Child 之间有一个简单的 OneToMany 关系。
Parent:
@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可以有很大的children 我想利用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 并且我正在为我的集合使用列表,因此从数据库加载 parent 会导致将 IndirectList 用于关系。只要我向 parent 添加另一个 child,我就可以看到执行 parent 的 children 的 select 查询。
我该如何更改?
我正在使用 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)
后,我能够看到第 206 行对 org.eclipse.persistence.indirection.IndirectList.shouldAvoidInstantiation()
的方法调用返回了 false,因为第 1007 行 org.eclipse.persistence.indirection.IndirectList._persistence_getPropertyChangeListener()
returns null 并且 null 不是 instanceof AttributeChangeListener。因此,该关系随后由第 216 行中的 org.eclipse.persistence.indirection.IndirectList.getDelegate()
实例化。
对我来说这似乎是一个错误,但我对这个实现的了解还不够确定。
需要更改跟踪以支持在进行修改时不实例化惰性集合。按照此处所述使用编织时启用更改跟踪:https://www.eclipse.org/eclipselink/documentation/2.5/concepts/app_dev007.htm
我在 Parent 和 Child 之间有一个简单的 OneToMany 关系。
Parent:
@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可以有很大的children 我想利用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 并且我正在为我的集合使用列表,因此从数据库加载 parent 会导致将 IndirectList 用于关系。只要我向 parent 添加另一个 child,我就可以看到执行 parent 的 children 的 select 查询。
我该如何更改?
我正在使用 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)
后,我能够看到第 206 行对 org.eclipse.persistence.indirection.IndirectList.shouldAvoidInstantiation()
的方法调用返回了 false,因为第 1007 行 org.eclipse.persistence.indirection.IndirectList._persistence_getPropertyChangeListener()
returns null 并且 null 不是 instanceof AttributeChangeListener。因此,该关系随后由第 216 行中的 org.eclipse.persistence.indirection.IndirectList.getDelegate()
实例化。
对我来说这似乎是一个错误,但我对这个实现的了解还不够确定。
需要更改跟踪以支持在进行修改时不实例化惰性集合。按照此处所述使用编织时启用更改跟踪:https://www.eclipse.org/eclipselink/documentation/2.5/concepts/app_dev007.htm