I get a DanglingHREFException: e.g., "org.eclipse.emf.ecore.xmi.DanglingHREFException: The object 'com.example.Foo@2f5dda ()' is not contained in a resource." What do I need to do?
All objects need to be contained by a resource in order to be serialized. That implies that for EObject x to be serialized, it must be the case that x.eResource() != null. The resource will be non-null if either x is directly contained by a resource, i.e., if for Resource r, r.getContents().contains(x), or if x has a container that in turn is contained by a resource, i.e., if for Resource r, r.getContents().contains(x.eContainer()). This definition is recursive. So an object must be contained by a resource directly, or indirectly by virtue of being part of a containment reference where, recursively, the container too must be contained by a resource.
So if you get the above exception, that means you have some object x contained by a resource r that refers to another object y, that is not directly or indirectly contained by any resource. It's possible to make the EReference feature used by x to refer to y be transient, so that it's not saved at all, or to make it a containment reference, so that y is contained by the same resource as x, but barring changes to the Ecore model itself, the one and only way to solve this problem is to add y to some resource, i.e., r.getContents().add(y), or to add/set it to some containment reference of an object z, i.e., z.getYs().add(y), where z is already contained by a resource, i.e., z.eResource() != null
转自:http://wiki.eclipse.org/index.php/EMF-FAQ#I_get_a_DanglingHREFException:_e.g..2C_.22org.eclipse.emf.ecore.xmi.DanglingHREFException:_The_object_.27com.example.Foo.402f5dda_.28.29.27_is_not_contained_in_a_resource..22_What_do_I_need_to_do.3F