During out application development we encountered a use case where we need
@Reference fields to refer identifiers other than the @Id field.
This is due to the way we manage versions of the documents in our collections -
while each document has its own unique identifier, different versions of the
same document have the same document identifier (we refer to it as assetId).
We had two requirements:
1. Be able to use @Reference to refer to a different field than the @Id field.
2. Be able to specify a more complex logic when reading the referenced document
- as stated, there are several versions on the same document, and we needed to
add the logic as to which one to read.
We've made several changes to the Morphia code to accommodate our needs, those
changes are attached as patches to this issue.
Here's an explanation of what was done:
1. The @Reference annotation has two new fields:
a. String referenceField() default Mapper.ID_KEY; // The field name used as the reference
b. Class<? extends ReferenceObjectResolver> objectResolver() default DefaultReferenceObjectResolver.class; // The object used to resolve the reference when reading the documents into memory.
2. ReferenceObjectResolver interface was introduced to support resolving of
objects based on custom logic.
3. DefaultReferenceObjectResolver which implements ReferenceObjectResolver was
introduced to support the original and basic resolving method for referenced
objects.
4. ReferenceMapper was modified to accommodate the following changes:
a. When saving an entity with @Reference fields, it now checks the referenceField to decide which field to use a reference.
b. When reading an entity, it now uses the objectResover field of the @Reference annotation in order to perform the reading logic.
We've been using these modifications for several months now and have had no
problems with them - they work exactly as planned.
There are, however, two limitations for the current modifications:
a. They do not apply for lazy-loading of @Reference fields - we have no need for that, and implementing it seemed a bit beyond of what we could understand from the code.
b. The may not apply for all data types applicable for @Reference. I think it applies well for a single value and List, but not for Map.
Please take the time to go over the suggestions and hopefully you'd incorporate
them in the actual Morphia driver.
Obviously, I'm here to answer any questions.
Thanks,
Ron
Original issue reported on code.google.com by Web...@gmail.com on 10 Mar 2012 at 4:21
Original issue reported on code.google.com by
Web...@gmail.com
on 10 Mar 2012 at 4:21Attachments: