我想使用MyCollection实现java提供的集合接口(interface)。它对于 EAGER 加载工作得很好,但是对于延迟加载 eclipselink 使用它自己的实现(IndirectList) 。是否可以使用 MyCollection 进行延迟加载?
MyCollection 代码位于此处:
package com.alta.convertor;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
public class MyCollection<E> implements Collection<E>,Cloneable{
Collection<E> collection;
@SuppressWarnings({ "unchecked", "rawtypes" })
public MyCollection() {
this(new Vector());
}
public MyCollection(Collection<E> collection) {
this.collection=collection;
}
@Override
public int size() {
return collection.size();
}
@Override
public boolean isEmpty() {
return collection.isEmpty();
}
@Override
public boolean contains(Object o) {
return collection.contains(o);
}
@Override
public Iterator<E> iterator() {
return collection.iterator();
}
@Override
public Object[] toArray() {
return collection.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return collection.toArray(a);
}
@Override
public boolean add(E e) {
return false;
}
@Override
public boolean remove(Object o) {
return collection.remove(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return collection.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends E> c) {
return collection.addAll(c);
}
@Override
public boolean removeAll(Collection<?> c) {
return collection.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return collection.retainAll(c);
}
@Override
public void clear() {
collection.clear();
}
}
我的实体类为:
@Entity
public class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="personid")
private Long personId;
private String firstName;
private String lastName;
private String email;
@OneToMany(cascade = CascadeType.ALL,mappedBy="person",targetEntity=Address.class,fetch=FetchType.EAGER)
私有(private) MyCollection 地址数组;
public Person() {
}
public Person(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public MyCollection<Address> getAddressArray() {
return addressArray;
}
public void setAddressArray(MyCollection<Address> address) {
this.addressArray = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getPersonId() {
return personId;
}
public void setPersonId(Long personId) {
this.personId = personId;
}
}
最佳答案
目前不行,您需要使用集合接口(interface)之一来允许 LAZY(List、Set、Map)。如果您使用 EAGER,则可以使用自己的集合类型。
但是,我目前正在解决这个问题,并且很可能很快会检查 2.6 的修复,
https://bugs.eclipse.org/bugs/show_bug.cgi?id=411819
一般来说,如果可以的话,使用集合接口(interface)仍然更好。 您还可以将关系包装在代表您的集合的 Embeddable 中。
关于java - 我们如何在 eclipseLink 中使用自己的集合进行延迟加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17365425/