---
title: "Object *** is not contained in a resource."
author: "Dollyn"
date: "2008-06-11T17:27:57Z"
tags: []
slug: "object-is-not-contained-in-a-resource"
draft: false
lastmod: "2008-06-11T17:27:57Z"
id: 202241
url: "https://dollyn.iteye.com/blog/202241"
---

### 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