Thanks Eitan. What you were saying half-way confirming what I was suspecting
(I am new to Castor, sorry if I ask dumb question here).
The reason why I said half-way is that I think we are obligated to put a root
class in order to generate XML and this root class will access the children
objects to recursively to generate correct XML.
That is why I am thinking an ArrayList can acting as root class(avoid me to write
a wrapper class). I am tried this, it did not work:
<class name="java.util.ArrayList">
<map-to xml="ROOT"/>
<field name="children" type="other" get-method="get" set-method="add" collection="set">
<bind-xml node="element" />
</field>
</class>
It complained about 'get' method not found, I guess it doesn't know how to pass a parameter.
So I tried get-method="toArray" and removed collection attribute. It started complaining about
multiple 'children' field.
Well I guess, I have to wrap the collection class and give it a try. I just worried wrapping JDK
class, but anyhow....
What I wish is the following(which should be relatively easy)
<class name="java.util.ArrayList" >
<map-to xml="ROOT" collection="set">
</class>
Then Castor see the collection flag then just use the well known collection routine to spill out XML
fields.
But I guess the difficulty relies that it is no longer persistable to sql. But I don't think it
matters since the all the child objects can generate their own sql, and castor can batch these sqls.
Does it sound a reasonable Request Feature Ehancement(RFE)?
Thanks.
-----Original Message-----
From: Eitan Suez [mailto:[EMAIL PROTECTED]]
Sent: Thursday, October 24, 2002 2:03 PM
To: Pengyu Wang
Subject: Re: [castor-dev] how to write mapping for collections
i believe castor will properly handle marshalling a class
that has a 1-many relationship with another class.
so for example, if you have a class named ObjectC
which has a property called 'children' that is a collection
of objects, then when you marshal objectC, you'll see
the children marshaled properly in the xml.
the mapping for objectc would include a
<field name="children" collection="collection" type="x" />
the type of the children must be specified. this can
be an interface that they all adhere to or a parent
class that they all extend. but i believe they must
be related in some way.
i hope this helps.
/ eitan
----- Original Message -----
From: Pengyu Wang
To: [EMAIL PROTECTED]
Sent: Thursday, October 24, 2002 3:28 PM
Subject: [castor-dev] how to write mapping for collections
Hi, I am wondering how I can write mapping file of collections. Basically I have ObjectA and ObjectB, I put them into collections such as ArrayList(which will be the root).
How do I write mapping file for the root object which is an arraylist?
I tried this it did not work:
ArrayList list = new ArrayList();
list.add(objectA);
list.add(objectB);
StringWriter sw = new StringWriter();
Marshaller marshaller = new Marshaller(sw);
marshaller.setMapping(mapping);
marshaller.marshal(list);
System.out.println(sw.toString());
The DOS console printout "<arrayList/>"
In the mapping.xml I have:
<mapping>
<class name="ObjectA">
<map-to xml="ObjectA"/>
....
<class name="ObjectB">
....
</class>
</mapping>
Thanks.
