lazy references causing queries to mongo

62 views
Skip to first unread message

Greg Rogers

unread,
Oct 11, 2012, 7:57:02 PM10/11/12
to mor...@googlegroups.com
I believe I'm seeing lots of unexpected queries from my morphia process over to my mongodb for lazy objects.  Please confirm or correct my understanding and advise if there is a way to avoid these queries.

Here's the setup:

class Foo has an embedded list of class Bar objects.   Class Bar has a lazy=true reference to class Baz.

@Entity("foo")
Class Foo {
   @Embedded
    private List<Bar> bars = Lists.newArrayList();

Class Bar:
   @Reference(lazy=true) Baz baz;


When an instance of foo is loaded, the list of Bar instances is created (I can see a @PreLoad method call happening).  It looks like the creation of the lazy reference proxy for baz is verifying that the id of the lazy object is a valid id by sending a query to mongo.  I can see these queries appear in the db.system.profile.find output.

The problem I'm running into is that even with a short list fo Bar instances, each instance is causing a unique, independent query to mongo.  Worse yet, in my actual class I have other lazy references as well to other classes (collections) and they two are getting validated by individual queries.

As you might guess, the number of round trips to the mongo server is starting to have in impact on the performance.  


The odd thing is that I don't see similar queries for direct lazy references.  I.e. Foo also has @Reference(lazy=true) List<BlargleBean> blargles.  That is, I only see these unexpected queries for lazy references in instances that are embedded.


Questions:
1. is my understanding correct that morphia makes a query to mongo to confirm that the id from a reference is good before creating the lazy proxy instance?

2. If so, is there any way to avoid this?


At this point I'm looking to refactor my data model to stop using references altogether and store only the ObjectIds and manage loading them as my app needs. 

Thanks
greg

James Green

unread,
Oct 15, 2012, 2:31:04 AM10/15/12
to mor...@googlegroups.com
Hi,

I've had a brief look at the code in my forked version. It seems Morphia internally maintains a map of referenced object ids. While the behaviour below sounds inconsistent at best, can you confirm that where no database scan is made (direct reference), Morphia will not already have had access to that ObjectId?

I am speculating, still getting my head around the internal structures at the moment...

James
Reply all
Reply to author
Forward
0 new messages