1

我想使用 MyCollection 实现 java 提供的集合接口。它对于 EAGER 加载工作正常,但对于延迟加载,eclipselink 使用它自己的implemntation(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;
    }

}
4

1 回答 1

2

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

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

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

一般来说,如果可以的话,最好还是使用集合接口。您还可以将关系包装在代表您的收藏的 Embeddable 中。

于 2013-07-02T14:47:45.483 回答