Why is my ForeignCollection always empty?

1,131 views
Skip to first unread message

Salil Wadnerkar

unread,
May 14, 2013, 6:58:53 AM5/14/13
to ormlit...@googlegroups.com
Hi,

I have a table readings which has references devices table.

I define the relation in my device class like this:
    @ForeignCollectionField(eager = true, foreignFieldName = "device") # I tried it with default - lazy first, And it did not work.
    ForeignCollection<Reading> readings;

And, in reading class like this:
    @DatabaseField(canBeNull = false, foreign = true, columnName = "device_mac_addr", foreignColumnName = "mac_addr")
    private Device device;

My query code is this:
        List<Device> devices = (List<Device>)deviceDao.queryForEq("mac_addr", deviceMacAddr);
        if (!devices.isEmpty()) {
            ForeignCollection<Reading> readings = devices.get(0).getReadings();

I get the correct device. But, the readings are always empty. Could someone tell me why this is happening?

Thanks
Salil

Moritz Pfeiffer

unread,
May 14, 2013, 7:05:47 AM5/14/13
to ormlit...@googlegroups.com
Hi Salil

You need to call deviceDao.refresh(yourDevice) in order to populate your foreign collections.

Cheers
Moritz

--
You received this message because you are subscribed to the Google Groups "ORMLite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ormlite-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Salil Wadnerkar

unread,
May 14, 2013, 9:30:46 AM5/14/13
to ormlit...@googlegroups.com
Hi Moritz,

My understanding is that refresh is necessary only if I modify the collection - via some other means than deviceDao.
If I am querying device from database, is the refresh necessary on all foreign-collections?
Anyway, I will try out your suggestion.

Thanks
Salil


--
You received this message because you are subscribed to a topic in the Google Groups "ORMLite Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ormlite-user/6UMQuNEw9b8/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to ormlite-user...@googlegroups.com.

Moritz Pfeiffer

unread,
May 14, 2013, 9:33:30 AM5/14/13
to ormlit...@googlegroups.com
Hi Salil

As far as I know it is necessary to refresh foreign collections after a query.
You could also mark your foreign collection as foreignAutoRefresh=true, but I've never done that so I don't know exactly how that behaves.

Greetings
Moritz

Salil Wadnerkar

unread,
May 14, 2013, 8:36:48 PM5/14/13
to ormlit...@googlegroups.com
Hi Moritz,

The refresh does not help. Here is my modified code:

        List<Device> devices = (List<Device>)deviceDao.queryForEq("mac_addr", deviceMacAddr);
        if (!devices.isEmpty()) {
            Device device = devices.get(0);
            deviceDao.refresh(device); // <- REFRESH here
            ForeignCollection<Reading> readings = device.getReadings();
            CloseableIterator<Reading> iterator = readings.closeableIterator();
            List<Reading> result = new ArrayList<Reading>();
            while (iterator.hasNext()) {
                Reading reading = iterator.next();
                result.add(reading);
            }
            iterator.close();
            return result;

It still returns an empty collection.

regards
Salil

Moritz Pfeiffer

unread,
May 15, 2013, 5:02:45 AM5/15/13
to ormlit...@googlegroups.com
Hi Salil

The problem might be with how you setup/populate your foreign collection in the first place.
Does your Reading class have a database field called device with foreign=true?
Then when you instantiate your Reading initially do you associate it with the device properly like this?
Reading theReading = new Reading();
… set some values …
theReading.device = … the device created with deviceDao.create(theDevice) …
readingDao.create(theReading)

I believe the relation to the device has to be set before you call readingDao.create(theReading), but don't quote me on that.


BTW you can just create an ArrayList from the foreign collection like this:
ArrayList<Reading> result = new ArrayList<Reading>(device.getReadings());
As ForeignCollection is simply a Collection.

Greetings
Moritz

Salil Wadnerkar

unread,
May 15, 2013, 6:08:58 AM5/15/13
to ormlit...@googlegroups.com
Hi Moritz,

Yes, my Reading class has a device field, defined like this:

@DatabaseField(canBeNull = false, foreign = true, columnName = "device_mac_addr", foreignColumnName = "mac_addr")
private Device device;

I am not instantiating any readings directly. I need to get the readings from the 'correct' device specified by the Mac address. So, I am relying on the ForeignCollection field of the device to get the appropriate device readings.

And, thanks for the tip for generating ArrayList from Collection. :)

regards
Salil

Moritz Pfeiffer

unread,
May 15, 2013, 6:31:39 AM5/15/13
to Salil Wadnerkar, ormlit...@googlegroups.com
Hi Salil

Readings aren't instantiated for you automatically.
Somewhere in your code you'll have to create them and associate them with the appropriate device so that the device's foreign collection gets populated properly.

Greetings
Moritz


Salil Wadnerkar <rohs...@gmail.com> schrieb:

Salil Wadnerkar

unread,
May 15, 2013, 6:35:20 AM5/15/13
to Moritz Pfeiffer, ormlit...@googlegroups.com
Hi Moritz,

I meant to say that devices and readings are already there in the database. I am merely querying the device (which is successful) and the corresponding readings through the ForeignCollection field of that device (which is not successful).

regards
Salil

Moritz Pfeiffer

unread,
May 15, 2013, 6:50:29 AM5/15/13
to Salil Wadnerkar, ormlit...@googlegroups.com
Hi Salil

I understand the problem as being your inability to access the device's readings.
Perhaps the relations in your db aren't properly configured?
How did the Readings get into the db?
And when the readings were inserted into the db was the relationship between the reading and the device properly established?
I suspect that the readings are missing the foreign key of the associated device.

Moritz Pfeiffer

unread,
May 15, 2013, 3:33:33 AM5/15/13
to ormlit...@googlegroups.com
Hi Salil

The problem might be with how you setup/populate your foreign collection in the first place.
Does your Reading class have a database field called device with foreign=true?
Then when you instantiate your Reading initially do you associate it with the device properly like this?
Reading theReading = new Reading();
… set some values …
theReading.device = … the device created with deviceDao.create(theDevice) …
readingDao.create(theReading)

I believe the relation to the device has to be set before you call readingDao.create(theReading), but don't quote me on that.


BTW you can just create an ArrayList from the foreign collection like this:
ArrayList<Reading> result = new ArrayList<Reading>(device.getReadings());
As ForeignCollection is simply a Collection.

Greetings
Moritz

Tom Maxwell-Mans

unread,
Jul 18, 2014, 1:40:51 AM7/18/14
to ormlit...@googlegroups.com
Hey I'm having the exact same problem, so I'm wondering if this problem got solved?

 If it did I would really be interested in the answer 

thank you.

Peter Lastameder

unread,
Jan 19, 2016, 9:53:34 AM1/19/16
to ORMLite Users
I think I have the same issue... Did you solve this problem?
Reply all
Reply to author
Forward
0 new messages