Hello ,

I use Castor to deal with XML/JAVA/DB transformations.
I have problem mapping 1:n . 

This is the schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";>
        <xsd:element name="State">
                <xsd:complexType>
                        <xsd:all>
                                <xsd:element ref="Body"/>
                        </xsd:all>
                        <xsd:attribute name="StateName" type="xsd:string" 
use="optional"/>
                        <xsd:attribute name="ID_State" type="xsd:integer" 
use="optional"/>
                </xsd:complexType>
        </xsd:element>
        <xsd:element name="Body">
                <xsd:complexType>
                        <xsd:sequence>
                                <xsd:element ref="Column" maxOccurs="unbounded"/>
                        </xsd:sequence>
                        <xsd:attribute name="ID_Body" type="xsd:integer" 
use="optional"/>
                        <xsd:attribute name="ID_State" type="xsd:integer" 
use="optional"/>
                </xsd:complexType>
        </xsd:element>
        <xsd:element name="Column">
                <xsd:complexType>
                        <xsd:attribute name="ID_Column" type="xsd:integer" 
use="optional"/>
                        <xsd:attribute name="ID_Body" type="xsd:integer" 
use="optional"/>
                </xsd:complexType>
        </xsd:element>

</xsd:schema>


This is the class BODY that was generated by Castor:


public class Body implements java.io.Serializable {

    private int _ID_Body;
    private boolean _has_ID_Body;
    private int _ID_State;
    private boolean _has_ID_State;
    private java.util.Vector _columnList;

    public Body() {
        super();
        _columnList = new Vector();
    } 

public void addColumn(Column vColumn)
        throws java.lang.IndexOutOfBoundsException
    {
        _columnList.addElement(vColumn);
    } 
public void addColumn(int index, Column vColumn)
        throws java.lang.IndexOutOfBoundsException
    {
        _columnList.insertElementAt(vColumn, index);
    } //-- void addColumn(int, Column) 

    public void deleteID_Body()
    {
        this._has_ID_Body= false;
    } //-- void deleteID_Body() 

    public void deleteID_State()
    {
        this._has_ID_State= false;
    } //-- void deleteID_State() 

   public java.util.Enumeration enumerateColumn()
    {
        return _columnList.elements();
    } //-- java.util.Enumeration enumerateColumn() 

    public Column getColumn(int index)
        throws java.lang.IndexOutOfBoundsException
    {
        //-- check bounds for index
        if ((index < 0) || (index > _columnList.size())) {
            throw new IndexOutOfBoundsException();
        }
        
        return (Column) _columnList.elementAt(index);
    } //-- Column getColumn(int) 

    public Column[] getColumn()
    {
        int size = _columnList.size();
        Column[] mArray = new Column[size];
        for (int index = 0; index < size; index++) {
            mArray[index] = (Column) _columnList.elementAt(index);
        }
        return mArray;
    } //-- Column[] getColumn() 
    
     public int getColumnCount()
    {
        return _columnList.size();
    } //-- int getColumnCount() 

    
    public int getID_Body()
    {
        return this._ID_Body;
    } //-- int getID_Body() 

    public int getID_State()
    {
        return this._ID_State;
    } //-- int getID_State() 

    public boolean hasID_Body()
    {
        return this._has_ID_Body;
    } //-- boolean hasID_Body() 

    public boolean hasID_State()
    {
        return this._has_ID_State;
    } //-- boolean hasID_State() 

    public boolean isValid()
    {
        try {
            validate();
        }
        catch (org.exolab.castor.xml.ValidationException vex) {
            return false;
        }
        return true;
    }
    public void marshal(java.io.Writer out)
        throws org.exolab.castor.xml.MarshalException,
org.exolab.castor.xml.ValidationException
    {
        
        Marshaller.marshal(this, out);
    }
    public void marshal(org.xml.sax.DocumentHandler handler)
        throws org.exolab.castor.xml.MarshalException,
org.exolab.castor.xml.ValidationException
    {
        
        Marshaller.marshal(this, handler);
    }
    public void removeAllColumn()
    {
        _columnList.removeAllElements();
    }
    public Column removeColumn(int index)
    {
        Object obj = _columnList.elementAt(index);
        _columnList.removeElementAt(index);
        return (Column) obj;
    }
    public void setColumn(int index, Column vColumn)
        throws java.lang.IndexOutOfBoundsException
    {
         if ((index < 0) || (index > _columnList.size())) {
            throw new IndexOutOfBoundsException();
        }
        _columnList.setElementAt(vColumn, index);
    }
    public void setColumn(Column[] columnArray)
    {
        //-- copy array
        _columnList.removeAllElements();
        for (int i = 0; i < columnArray.length; i++) {
            _columnList.addElement(columnArray[i]);
        }
    }
    public void setID_Body(int ID_Body)
    {
        this._ID_Body = ID_Body;
        this._has_ID_Body = true;
    }
    public void setID_State(int ID_State)
    {
        this._ID_State = ID_State;
        this._has_ID_State = true;
    }
    public static com.acsior.sitegen.xml.api.Body
unmarshal(java.io.Reader reader)
        throws org.exolab.castor.xml.MarshalException,
org.exolab.castor.xml.ValidationException
    {
        return (com.acsior.sitegen.xml.api.Body)
Unmarshaller.unmarshal(com.acsior.sitegen.xml.api.Body.class, reader);
    }
    public void validate()
        throws org.exolab.castor.xml.ValidationException
    {
        org.exolab.castor.xml.Validator validator = new
org.exolab.castor.xml.Validator();
        validator.validate(this);
    }

}



How to map Column to Body? There can be many Columns in one Body.




<?xml version="1.0"?>
<mapping xmlns="http://castor.exolab.org/";>
        <description>Castor generated mapping file</description>

        <class name="com.acsior.sitegen.xml.api.State" identity="ID_State"
access="shared">
                <description>Default mapping for class
com.acsior.sitegen.xml.api.State</description>
                <map-to table="sm_State"  xml="State"/>
                <field name="stateName" type="java.lang.String">
                        <sql name="_stateName" type="integer"/>
                        <bind-xml name="StateName" node="attribute"/>
                </field>
                <field name="ID_State" type="int">
                <sql name="ID_State" type="integer"/>
                        <bind-xml name="ID_State" node="attribute"/>
                </field>
                <field name="body" type="com.acsior.sitegen.xml.api.Body"
required="true">
                        <sql name="ID_Body"></sql>
                        <bind-xml name="Body" node="element"/>
                </field>

        </class>
        <class name="com.acsior.sitegen.xml.api.Column" identity="ID_Column"
access="shared">
                <description>Default mapping for class
com.acsior.sitegen.xml.api.Column</description>
                <map-to table="sm_Column"  xml="Column"/>
                <field name="ID_Column" type="int">
                        <sql name="ID_Column" type="integer"/>
                        <bind-xml name="ID_Column" node="attribute"/>
                </field>
                <field name="ID_Body" type="int">
                        <sql name="ID_Body" type="integer"/>
                        <bind-xml name="ID_Body" node="attribute"/>
                </field>

        </class>
        <class name="com.acsior.sitegen.xml.api.Body" identity="ID_Body"
access="shared">
                <description>Default mapping for class
com.acsior.sitegen.xml.api.Body</description>
                <map-to table="sm_Body"  xml="Body"/>
                <field name="ID_Body" type="int">
                        <sql name="ID_Body" type="integer"/>
                        <bind-xml name="ID_Body" node="attribute"/>
                </field>
                <field name="ID_State" type="int">
                        <sql name="ID_sTATEy" type="integer"/>
                        <bind-xml name="ID_State" node="attribute"/>
                </field>
                
                
                <!-- THERE IS THE PROBLEM! -->

                <field name="_columnList" type="com.acsior.sitegen.xml.api.Column"
required="true" collection="enumerate">
                        <bind-xml name="Column" node="element"/>
                </field>
                <container/>
        </class>
</mapping>

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to