PDA

View Full Version : Lists with DBRef have size 1 but get(0) returns null?



vguna
Sep 9th, 2011, 03:55 PM
Hi.

I'm using M4 together with mongodb 1.83. I've got an Object A that has:

@DBRef
List<B> bs = new ArrayList<B>();

Saving B, adding B to A and saving A writes the dbref to the mongodb.
But findOne on A and looking at the size of List<B> returns 1. Calling get(0) on that List returns null though.

A and B are separate Collections in mongodb. Looking at B in the db shows the correct attributes.

Any idea why that happens? I assume that DbRefs (more precise: related Objects) get eagerly loaded automatically when their parent is loaded. Is that correct?

Thanks in advance.

johannz
Sep 9th, 2011, 06:44 PM
This is just a guess.

It looks like the DBRefs are being stored as {"$ref" : "b", "$id": "4e6aa2ed8d4d11bdb2402555"} but the object B is stored as { "_id" : ObjectId("4e6aa2ed8d4d11bdb2402555"), blah...}

When it tries to instantiate the objects, it tries to find them by id, which it thinks is "4e6aa2ed8d4d11bdb2402555, instead of the real id of ObjectId("4e6aa2ed8d4d11bdb2402555").

Note: the difference is looking for a string vs looking for an ObjectId.

This looks like it is a bug.

It also happens on non-collection DBRef's, as I've painfully found out today.

Created https://jira.springsource.org/browse/DATADOC-275 to track this.

vguna
Sep 10th, 2011, 03:39 AM
Hm that are bad news. Can I use M3 for a quick "workaround" so I can go on with my work? Or gets it even worse then :)?

vguna
Sep 10th, 2011, 06:42 AM
Hm that are bad news. Can I use M3 for a quick "workaround" so I can go on with my work? Or gets it even worse then :)?

That worked for me. Well knowing that other things may be broken....

johannz
Sep 30th, 2011, 05:58 PM
The bug https://jira.springsource.org/browse/DATADOC-275 has been marked resolved, but no notes on what Oliver Gierke did to resolve it.

I guess we'll see when they release 1.0 RC1-Mongo