Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORMLite LazyForeignCollection: How to query a collection only once?

Tags:

ormlite

Let's assume I have the following object:

public class MyOwnList {

    @DatabaseField(id= true)
    protected int id;

    @ForeignCollectionField(eager = false)
    protected Collection<Item> items;
}

As items is marked as lazy it won't be loaded if I load the list object from the database. That's exactly what I want!!

The problem is that everytime I access items, ORMLite makes a sql query to get the collection. Only discovered it after activating the logging of ORMLite...

Why does it do that? Any good reason for that?

Is there any way that I can lazy load the collection, but only once, not everytime I access the collection? So something between eager and lazy?

like image 797
d1rk Avatar asked Jul 24 '12 18:07

d1rk


1 Answers

The problem is that everytime I access items, ORMLite makes a sql query to get the collection.

So initially I didn't understand this. What you are asking for is for ORMLite to cache the collection of items after it lazy loads it the first time. The problem with this as the default is that ORMLite has no idea how big your collection of items is. One of the reasons why lazy collections are used is to handle large collections. If ORMLite kept all lazy collections around in memory, it could fill the memory.

I will add to the TODO list something like lazyCached = true which does a hybrid between lazy and eager. Good suggestion.

like image 196
Gray Avatar answered Oct 16 '22 22:10

Gray