Lazy collections cannot be used if they have been deserialized

1.9k views Asked by At

I'm using ORM Lite in my project, and I have a situation where step by putExtra the intent of a Person object, which has a Collection of Email object, but when I try to read this Collection which is like LazyLoad (eager = false) below follows the models and the code used to retrieve the collection of Email:

public class Pessoa implements Serializable{

    @DatabaseField(generatedId=true)
    private int id;

        ...

    @ForeignCollectionField(eager=false)
    private Collection<Email> emails;

        ...
}
public class Email implements Serializable{

    @DatabaseField(generatedId=true)
    private int id;

    ...

    @DatabaseField(foreign=true)
    private Pessoa pessoa;

       ...
}

Retrieving the Collection:

if(getIntent().hasExtra("Pessoa")){
       pessoa = (Pessoa) getIntent().getSerializableExtra("Pessoa");

Iterator<Endereco> iterator = pessoa.getEnderecos().iterator();
while(iterator.hasNext()){
        Endereco end = iterator.next();
        Log.e("ENDERECO",end.getLogradouro());
}

I get the following error:

12-18 11:48:08.168: E/AndroidRuntime(26028): FATAL EXCEPTION: main 12-18 11:48:08.168: E/AndroidRuntime(26028): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pedidosexample/com.example.abertura.CadastroVisualizacao}: java.lang.IllegalStateException: Internal DAO object is null. Lazy collections cannot be used if they have been deserialized. 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.ActivityThread.access$600(ActivityThread.java:128) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.os.Handler.dispatchMessage(Handler.java:99) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.os.Looper.loop(Looper.java:137) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.ActivityThread.main(ActivityThread.java:4517) 12-18 11:48:08.168: E/AndroidRuntime(26028): at java.lang.reflect.Method.invokeNative(Native Method) 12-18 11:48:08.168: E/AndroidRuntime(26028): at java.lang.reflect.Method.invoke(Method.java:511) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 12-18 11:48:08.168: E/AndroidRuntime(26028): at dalvik.system.NativeStart.main(Native Method) 12-18 11:48:08.168: E/AndroidRuntime(26028): Caused by: java.lang.IllegalStateException: Internal DAO object is null. Lazy collections cannot be used if they have been deserialized. 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.j256.ormlite.dao.LazyForeignCollection.seperateIteratorThrow(LazyForeignCollection.java:310) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.j256.ormlite.dao.LazyForeignCollection.iteratorThrow(LazyForeignCollection.java:71) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.j256.ormlite.dao.LazyForeignCollection.closeableIterator(LazyForeignCollection.java:60) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.j256.ormlite.dao.LazyForeignCollection.iterator(LazyForeignCollection.java:47) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.j256.ormlite.dao.LazyForeignCollection.iterator(LazyForeignCollection.java:28) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.example.abertura.CadastroVisualizacao.preencheCampos(CadastroVisualizacao.java:85) 12-18 11:48:08.168: E/AndroidRuntime(26028): at com.example.abertura.CadastroVisualizacao.onCreate(CadastroVisualizacao.java:53) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.Activity.performCreate(Activity.java:4470) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) 12-18 11:48:08.168: E/AndroidRuntime(26028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 12-18 11:48:08.168: E/AndroidRuntime(26028): ... 11 more

The error clearly says what the problem is, but it has another way to pass the Person object to another intent and that works with lite ORM?

Thank you!

1

There are 1 answers

1
lvzhendong On

@ForeignCollectionField(eager = true)