Thursday, April 28, 2016

Castor and Castor Mapping

When using Castor it will parse your castor mapping xml linearly. For example if you had this class structure:
package com.sample.ixml;
public Sample {
     SubclassB sB;
}
public class SubclassB extends SuperclassA {
     String strB;
}
public class SuperclassA {
     String strA;
}  

Along with this xml castor mapping:

<class name="com.sample.ixml.Sample">
     <map-to xml="Sample" />
     <field name="sB" type="com.sample.ixml.Sample">
          <bind-xml name="SubclassB" />
     </field>
</class>
<class name="com.sample.ixml.SubclassB" extends="com.sample.ixml.SuperclassA">
     <map-to xml="SuperclassB" />
     <field name="strB" type="string">
          <bind-xml name="StrB" />
     </field>
</class>
<class name="com.sample.ixml.SuperclassA">
     <map-to xml="SuperclassA" />
     <field name="strA" type="string">
          <bind-xml name="StrA" />
     </field>
</class>

It will trigger an exception with the message "forward references are not supported". In order to fix this you have to reorder your mapping so that it looks like this:
<class name="com.sample.ixml.Sample">
     <map-to xml="Sample" />
     <field name="sB" type="com.sample.ixml.Sample">
          <bind-xml name="SubclassB" />
     </field>
</class>
<class name="com.sample.ixml.SuperclassA">
     <map-to xml="SuperclassA" />
     <field name="strA" type="string">
          <bind-xml name="StrA" />
     </field>
</class>
<class name="com.sample.ixml.SubclassB" extends="com.sample.ixml.SuperclassA">
     <map-to xml="SuperclassB" />
     <field name="strB" type="string">
          <bind-xml name="StrB" />
     </field>
</class>

No comments: