为什么MorphiaIterator只能迭代一次?

时间:2014-05-06 22:14:42

标签: java mongodb morphia

我正在使用Morphia 0.107。

为什么MorphiaIterator类的构建方式只能迭代一次?

import com.mongodb.MongoClient;
import java.net.UnknownHostException;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.Query;

public class App {

    public static void main(String[] args) throws UnknownHostException {

        Morphia m = new Morphia();
        Datastore ds = m.createDatastore(new MongoClient(), "test");

        Query<Person> q = ds.createQuery(Person.class);

        // MorphiaIterator
        Iterable<Person> iterable = q.fetchEmptyEntities();
        System.out.println(iterable.iterator().hasNext());
        for (Person person : iterable) {
            System.out.println(person);
        }

        System.out.println(iterable.iterator().hasNext());
        for (Person person : iterable) {
            System.out.println(person);
        }

        // This should work, but throws java.util.NoSuchElementException
        ds.delete(Person.class, q.fetchKeys());

    }

}

0 个答案:

没有答案