I ran into this same problem.
It seems that Castor does not handle native arrays very well.
It may be because the testing has been done using ArrayLists 
instead. 

This lack of support for arrays is unfortunate because I think 
that castor should support the JavaBeans specification that calls 
for array type obects as collections.  This is particularly important
for integration with other products such as Struts and Apache Axis
that require JavaBeans compliance. It seems to me that one of the
primary motivating factors for using JDO is that it should be able
to serialize and deserialize JavaBeans compliant object graphs.

JavaBeans should, I think, provide Collections compliant accessor 
mehthods but that is another story. 

I have worked around this by providing two sets of methods for
dealing with the attribute:

I store the attribute as an ArrayList because it is more
convenient but I am sure you could store it as an array if you
wanted to rework the methods a little.

BTW I am using CastorDoclet. I recommend to all castor users to 
use CastorDoclet. It's great.

The trick is basically to specify in the mapping file ArrayList
compliant methods for Castor to use and retain JavaBeans compliant
array methods for other uses.

BTW the error is in FieldMolder. In the array of CollectionInfos 
the array is mapped to Object[].class which is java.lang.Object.

    private static CollectionInfo[] info = 
        { new CollectionInfo( "collection", java.util.Collection.class ),
          new CollectionInfo( "vector", java.util.Vector.class ),
          new CollectionInfo( "arraylist", java.util.ArrayList.class ),
          new CollectionInfo( "hashtable", java.util.Hashtable.class ),
          new CollectionInfo( "hashmap", java.util.HashMap.class ),
          new CollectionInfo( "set", java.util.Set.class ),
          new CollectionInfo( "hashset", java.util.HashSet.class ),
          new CollectionInfo( "map", java.util.Map.class ),
          new CollectionInfo( "array", Object[].class ) };

when the getCollectionType is called with an array type as follows
java.lang.Object is returned. Castor then complains that the types
do not match. Given this error I concluded that it is better not to 
try to use array types in Castor. If I am wrong, someone please
let me know, I would love to use them directly.

    // ======================================================
    //  copy from FieldHanlder.java and modified
    // ======================================================
    protected Class getCollectionType( String coll, boolean lazy ) 
            throws MappingException {
        Class type;

        for ( int i=0; i<info.length; i++ ) {
            if ( info[i].name.equals( coll ) ) 
                return info[i].type;
        }
        return null;
        /*
        if ( "collection".equals( coll ) )
            type = ArrayList.class;
        else if ( "vector".equals( coll ) )
            type = Vector.class;
        else 
            throw new MappingException( "Unsupported collection type: " + coll );

        if ( lazy )
            return null;
        else 
            return type;*/
    }


//------------------------------------------------------
// My work around
//--------------------------------------------------------
     /**
     * 
     * @lazy
     * @field-type ObjectAttributeImpl
     * @many-key objectId
     * @get-method getObjectAttributesAsList
     * @set-method setObjectAttributesAsList
     * @sql-dirty check
     *
     */
    private ArrayList objectAttributes = new ArrayList ();
  
    //************************************************************
    // Collection based accessors for objectAttributes
    //************************************************************

    /** Getter for property objectAttributes as Collection.
     * @return Value of property objectAttributes.
     *
     */
    public java.util.ArrayList getObjectAttributesAsList() {
        return objectAttributes;
    }    
    
    /** Setter for property objectAttributes as List.
     * @param objectAttributes New value of property objectAttributes.
     *
     */
    public void setObjectAttributesAsList(java.util.ArrayList objectAttributes) {
        this.objectAttributes = objectAttributes;
    }    
    
    /** Adds an objectAttribute to the object
     */
    public void addObjectAttribute ( ObjectAttributeImpl oa ) {
        this.objectAttributes.add(oa);
    }
    
    /** removes an ObjectAttribute from the specified index
     */
    public void removeObjectAttribute ( int index ) {
        this.objectAttributes.remove( index );
    }    
    
    //************************************************************
    // JavaBeans compliant accessor methods for objectAttributes.
    //************************************************************
    
    /** Getter for property objectAttributes.
     * @return Value of property objectAttributes.
     *
     */
    public ObjectAttributeImpl[] getObjectAttributes() {
        return ( ObjectAttributeImpl[] )objectAttributes.toArray( oaExemplar );
    }    
    
    /** Setter for property objectAttributes.
     * @param objectAttributes New value of property objectAttributes.
     *
     */
    public void setObjectAttributes(ObjectAttributeImpl[] objectAttributes) {
        this.objectAttributes = (ArrayList) Arrays.asList( objectAttributes );
    }
    
    /** Indexed getter for objectAttributes
     */
    public ObjectAttributeImpl getObjectAttributes( int index ) {
        return ( ObjectAttributeImpl ) objectAttributes.get( index );
    }
    
    /** Indexed setter fo ObjectAttirbutes
     */
    public void setObjectAttributes (ObjectAttributeImpl oa, int index ) {
        objectAttributes.set( index, oa );
    }

-----Original Message-----
From: Neil Blue [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, November 19, 2002 2:16 AM
To: [EMAIL PROTECTED]
Subject: [castor-dev] How do I define an array collection for JDO?


Hello,

I am using Castor to unmarshall and store XML files. I have managed this for
a subset of elements from my schema so far. However I have come across a
problem trying to store collections of objects. 

Here is an extract from my schema:

<xs:complexType name="atomsType">
        <xs:sequence>
                <xs:element name="atom" type="atomType"
maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:integer"/>
</xs:complexType>

This creates an 'AtomsType' class with 'Atom[] getAtom()' and
'setAtom(Atom[])' methods. 

This works fine till I try to store this array. Here is my mapping.xml
entry:

        <class name="com.novobase.castor.Atoms" identity="id"
depends="com.novobase.castor.Minimol" key-generator="MAX">
        <description>Atoms</description>
                <map-to table="atoms"/>
    
                <field name="id">
                        <sql name="id"/>
                </field>
                
                <field name="atom" type="com.novobase.castor.Atom"
collection="array">
                        <sql many-key="parentId"/>
                </field>
        </class>

But the error I get when I go to store the object is:

    [junit] Testcase: testJDO took 1.035 sec
    [junit]     Caused an ERROR
    [junit] The return type for method public com.novobase.castor.Atom[]
com.novobase.castor.AtomsType.getAtom() does not match the declared field
type [Ljava.lang.Object;
    [junit] org.exolab.castor.mapping.MappingException: The return type for
method public com.novobase.castor.Atom[]
com.novobase.castor.AtomsType.getAtom() does not match the declared field
type [Ljava.lang.Object;
    [junit]     at
org.exolab.castor.persist.FieldMolder.findAccessor(Unknown Source)

Please could anyone tell me if my mapping is wrong or if there is a better
way to do this.

Thank you
Neil 

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

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

Reply via email to