Skip to main content

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

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
-- 
View this message in context: http://www.nabble.com/-MOXy--setting-bi-directional-references-during-unmarshalling-tp26032300p26032300.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top