Reputation: 6459
I'm coping and storing some data from a sql database. I store the data for object A in one pass, and I store object B in a different pass. Object B has A's _id saved in it. I would like to save A with a reference to B so that when I load A I get all the information in B at the same time.
However, object A will not be in my memory at the time I'm generating object B, not without huge refactoring. Since a reference object is little more then an _id anyways is there a way I can tell morphia that they should use A's _id to reference A without first loading all of A into memory to save the object into B? Would creating an otherwise 'empty' object with nothing but A's _id and every other value set to default and saving it effectively do what I want?
Upvotes: 0
Views: 590
Reputation: 6233
If you're working at the level morphia is, you're going to need an A object reference in hand. That said, it doesn't have to be the full A. e.g.:
B b = new B();
// some work here
b.setA(new A(idForA)));
datastore.save(b);
All morphia needs is the collection (which it gets from the mapping information for A) and the _id value for that A. What will happen here is when b
is saved, it will create Key (to keep it simple) for that A
and then create the DBRef
in the database using that value. Morphia doesn't care about any other state in an @Reference
member. So as long as it can pull out that ID to save, you should be all set.
Upvotes: 2