我们如何在eclipseLink中使用我们自己的集合进行Lazy加载?

时间:2013-06-28 13:06:52

标签: java eclipselink lazy-loading

我想使用MyCollection实现java提供的集合接口。它适用于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)    

私人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;
    }

}

1 个答案:

答案 0 :(得分:2)

目前还不需要使用其中一个集合接口来允许LAZY(List,Set,Map)。如果您使用EAGER,则可以使用自己的集合类型。

但是,我目前正在研究这个问题,并且很可能会很快检查到2.6的修复程序,

https://bugs.eclipse.org/bugs/show_bug.cgi?id=411819

一般情况下,如果可以,最好使用集合接口。 您还可以将关系包装在代表您的集合的Embeddable中。