Program: 65572
Project: 65572
Buggy source:
public void writeSingleValue(Object value,Object parent,XMLRecord row,AbstractSession session){
  DOMRecord record=(DOMRecord)row;
  Node root=record.getDOM();
  Object objectValue=value;
  if (this.getConverter() != null) {
    objectValue=getConverter().convertObjectValueToDataValue(objectValue,session,row.getMarshaller());
  }
  if (field != null) {
    root=XPathEngine.getInstance().create((XMLField)getField(),root,session);
  }
  org.w3c.dom.Document doc=record.getDocument();
  XMLField xmlRootField=null;
  boolean wasXMLRoot=false;
  Object originalObject=objectValue;
  Node toReplace=getNodeToReplace(root);
  if (usesXMLRoot() && (objectValue instanceof XMLRoot)) {
    xmlRootField=new XMLField();
    wasXMLRoot=true;
    XPathFragment frag=new XPathFragment();
    if ((((XMLRoot)objectValue)).getRootFragment().getNamespaceURI() != null) {
      frag.setNamespaceURI(((XMLRoot)objectValue).getNamespaceURI());
    }
 else {
      frag.setXPath(((XMLRoot)objectValue).getLocalName());
    }
    xmlRootField.setXPathFragment((((XMLRoot)objectValue)).getRootFragment());
    xmlRootField.setNamespaceResolver(row.getNamespaceResolver());
    objectValue=((XMLRoot)objectValue).getObject();
  }
  if (objectValue instanceof String) {
    writeSimpleValue(xmlRootField,record,session,originalObject,objectValue,root,toReplace,wasXMLRoot);
  }
 else   if (objectValue instanceof org.w3c.dom.Node) {
    Node importedCopy=doc.importNode((Node)objectValue,true);
    root.appendChild(importedCopy);
  }
 else {
    XMLDescriptor referenceDescriptor=(XMLDescriptor)session.getDescriptor(objectValue.getClass());
    if (referenceDescriptor == null) {
      writeSimpleValue(xmlRootField,record,session,originalObject,objectValue,root,toReplace,wasXMLRoot);
      return;
    }
    if (wasXMLRoot) {
      if (((XMLRoot)originalObject).getRootFragment().getNamespaceURI() != null) {
        String prefix=referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
        if ((prefix == null) || prefix.length() == 0) {
          prefix=row.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getRootFragment().getNamespaceURI());
        }
        if ((prefix == null) || prefix.length() == 0) {
          xmlRootField.getXPathFragment().setGeneratedPrefix(true);
          prefix=row.getNamespaceResolver().generatePrefix();
        }
        xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot)originalObject).getLocalName());
      }
    }
    DOMRecord nestedRecord=(DOMRecord)buildCompositeRow(objectValue,session,referenceDescriptor,row,xmlRootField,originalObject,wasXMLRoot);
    if ((nestedRecord != null) && (toReplace != null)) {
      if (nestedRecord.getDOM() != toReplace) {
        root.replaceChild(nestedRecord.getDOM(),toReplace);
      }
    }
 else     if (nestedRecord != null) {
      root.appendChild(nestedRecord.getDOM());
    }
 else     if (toReplace != null) {
      root.removeChild(toReplace);
    }
  }
}

Fixed version (ground truth):
public void writeSingleValue(Object value,Object parent,XMLRecord row,AbstractSession session){
  DOMRecord record=(DOMRecord)row;
  Node root=record.getDOM();
  Object objectValue=value;
  if (this.getConverter() != null) {
    objectValue=getConverter().convertObjectValueToDataValue(objectValue,session,row.getMarshaller());
  }
  if (field != null) {
    root=XPathEngine.getInstance().create((XMLField)getField(),root,session);
  }
  org.w3c.dom.Document doc=record.getDocument();
  XMLField xmlRootField=null;
  boolean wasXMLRoot=false;
  Object originalObject=objectValue;
  Node toReplace=getNodeToReplace(root);
  if (usesXMLRoot() && (objectValue instanceof XMLRoot)) {
    xmlRootField=new XMLField();
    wasXMLRoot=true;
    XPathFragment frag=new XPathFragment();
    if ((((XMLRoot)objectValue)).getNamespaceURI() != null) {
      frag.setNamespaceURI(((XMLRoot)objectValue).getNamespaceURI());
    }
    frag.setXPath(((XMLRoot)objectValue).getLocalName());
    xmlRootField.setXPathFragment(frag);
    xmlRootField.setNamespaceResolver(row.getNamespaceResolver());
    objectValue=((XMLRoot)objectValue).getObject();
  }
  if (objectValue instanceof String) {
    writeSimpleValue(xmlRootField,record,session,originalObject,objectValue,root,toReplace,wasXMLRoot);
  }
 else   if (objectValue instanceof org.w3c.dom.Node) {
    Node importedCopy=doc.importNode((Node)objectValue,true);
    root.appendChild(importedCopy);
  }
 else {
    XMLDescriptor referenceDescriptor=(XMLDescriptor)session.getDescriptor(objectValue.getClass());
    if (referenceDescriptor == null) {
      writeSimpleValue(xmlRootField,record,session,originalObject,objectValue,root,toReplace,wasXMLRoot);
      return;
    }
    if (wasXMLRoot) {
      if (((XMLRoot)originalObject).getNamespaceURI() != null) {
        String prefix=referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
        if ((prefix == null) || prefix.length() == 0) {
          prefix=row.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
        }
        if ((prefix == null) || prefix.length() == 0) {
          xmlRootField.getXPathFragment().setGeneratedPrefix(true);
          prefix=row.getNamespaceResolver().generatePrefix();
        }
        xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot)originalObject).getLocalName());
      }
    }
    DOMRecord nestedRecord=(DOMRecord)buildCompositeRow(objectValue,session,referenceDescriptor,row,xmlRootField,originalObject,wasXMLRoot);
    if ((nestedRecord != null) && (toReplace != null)) {
      if (nestedRecord.getDOM() != toReplace) {
        root.replaceChild(nestedRecord.getDOM(),toReplace);
      }
    }
 else     if (nestedRecord != null) {
      root.appendChild(nestedRecord.getDOM());
    }
 else     if (toReplace != null) {
      root.removeChild(toReplace);
    }
  }
}