Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] [MOXy] setting bi-directional references during unmarshalling

Hey Polly,

We're currently working on better support for handling bi-directional relationship mappings in EclipseLink. Currently in EclipseLink 1.2 (which is available today) for containment relationships, you can configure the mapping to automatically set the back-reference when it's populating the collection. This functionality isn't currently exposed in the MappingWorkbench but is available through the mapping APIs, Project XML and in JAXB through a custom annotation.

Using the mapping APIs you would do something like:
XMLCompositeCollectionMapping childMapping = new XMLCompositeCollectionMapping();
childMapping.setXPath("children/child");
childMapping.setReferenceClass(Child.class);
//Specify the name of the attribute on the reference class that points back to the container object
childMapping.setContainerAttributeName("parent");
childMapping.setContainerGetMethodName("getParent");
childMapping.setContainerSetMethodName("setParent");
parentDescriptor.addMapping(childMapping);

If you're using Project XML,  this can also be specified in the xml version of the project using the <container-attribute> element on the composite mapping.

In EclipseLink 1.2 this support is only availabe for Containment based mappings (XMLCompositeObjectMapping and XMLCompositeCollectionMapping), but EclipseLink 2.0 will have expanded support for bi-directional relationships to also include reference mappings.

Hope this helps,

-Matt

polly.c.chang wrote:
Hi,

I have a bi-directional relationship between Parent and Child classes like
this:

public class Parent {
    private List<Child> children;

    public List<Child> getChildren()
    public void setChildren(List<Child> children)
    public void addChild(final Child child) {
        this.getChildren().add(child);
	child.setParent(this);
    }
    public void removeChild(final Child child) {
        this.getChildren().remove(child);
	child.setParent(null);
    }
}

public class Child {
    private Parent parent;

    public void setParent(Parent parent)
    public Parent getParent()
}

When I unmarshal the Parent object, I find that the "children" collection
contains Child objects that do not have the back-reference to the Parent. 
This is probably because the Child objects were unmarshalled and set
directly into the "children" collection.  How do I change the behavior so
that EclipseLink MOXy calls the addChild() method for each Child object?  I
looked around EclipseLink Workbench and the schema, but I can't find any
mappings that look suitable.  Does MOXy support adding objects into a
collection by calling an add() method?  

If MOXy does not support calling an add() method for unmarshalling
collections, then the only workaround that I can think of is to turn on
"method accessing" for each bi-directional collection so that MOXy uses the
getter and setter methods for the list.  Then the setter needs to go through
the collection and fix up the back references to the Parent.  Usually my
setter is private/protected, so I assume that I'd have to make it public too
for EclipseLink to use it. 

What do you recommend?

Thanks!
--Polly
  

Back to the top