Jim,
Castor supports parent-child relationships via ID/IDREF and can be
expressed as such in a mapping file:
<class name="Foo" identity="id">
<field name="id" type="string"/>
</class>
<class name="Bar">
<field name="parent" type="Foo">
<bind-xml node="attribute" reference="true"/>
</field>
</class>
Note the identity="id" specified on the Foo class mapping. And also note
the reference="true" specified on the bind-xml element for the parent
field in the Bar class mapping.
I hope that helps.
--Keith
Jim Otte wrote:
>
> Ralf,
>
> Thanks for your reply!
> I tried your suggestion- by modifying the domain
> objects Foo and Bar- Now when I run the test class I
> get the following error:
>
> org.xml.sax.SAXException: unable to add 'bar' to <foo>
> due to the following exception:
> >>>--- Begin Exception ---<<<
> java.lang.StackOverflowError
> >>>---- End Exception ----<<<
> {file: [not available]; line: 6; column: 8}
>
> It seems as if Castor is not letting the parent
> elements be added to the child elements... I wonder if
> there is another way?
>
> Thanks for the help,
> Jim
>
> --- Ralf Joachim <[EMAIL PROTECTED]> wrote:
> >
> > Hey Jim,
> >
> > I am not an expert for castor XML, but have you
> > tried something like
> >
> > public class Foo {
> > ......
> > public void setBar(Bar bar) {
> > if (this.bar != bar) {
> > this.bar = bar;
> > bar.setFoo(this);
> > }}
> > ......
> > }
> >
> > public class Bar {
> > .......
> > public void setFoo(Foo foo) {
> > if (this.foo != foo) {
> > this.foo = foo;
> > foo.setBar(this);
> > }}
> > .......
> > }
> >
> > MfG
> > Ralf
> >
> >
> > ----- Original Message -----
> > From: "Jim Otte" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Tuesday, February 17, 2004 6:56 AM
> > Subject: [castor-dev] Obtaining refernceto Parent
> > class-Castor limitation??
> >
> >
> > >
> > > Castor Gurus:
> > >
> > > I have the following xml to be unmarshalled:
> > >
> > > <foo>
> > > <id>42</id>
> > > <bar>
> > > <greeting>hello world</greeting>
> > > </bar>
> > > </foo>
> > >
> > > into the following classes:
> > >
> > >
> > > public class Foo {
> > > public Bar bar;
> > > public String id;
> > >
> > > public Bar getBar() {
> > > return bar;
> > > }
> > >
> > >
> > > public void setBar(Bar bar) {
> > > this.bar = bar;
> > > }
> > >
> > >
> > > public String getId() {
> > > return id;
> > > }
> > >
> > > public void setId(String id) {
> > > this.id = id;
> > > }
> > >
> > > }
> > >
> > >
> > > and
> > >
> > >
> > > public class Bar {
> > >
> > > public String greeting;
> > > public Foo foo;
> > >
> > >
> > > public String getGreeting() {
> > > return greeting;
> > > }
> > >
> > >
> > > public void setGreeting(String greeting) {
> > > this.greeting = greeting;
> > > }
> > >
> > >
> > > public Foo getFoo() {
> > > return foo;
> > > }
> > >
> > >
> > > public void setFoo(Foo foo) {
> > > this.foo = foo;
> > > }
> > >
> > > }
> > >
> > > where in Bar I would like a reference to the
> > parent
> > > class Foo.
> > > I set up the mapping as such:
> > >
> > > <?xml version="1.0"?>
> > > <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object
> > > Mapping DTD Version 1.0//EN"
> > >
> > > "http://castor.exolab.org/mapping.dtd">
> > >
> > > <mapping>
> > >
> > > <class name="tudo.foobar.castor.domain.Foo">
> > > <map-to xml="foo"/>
> > >
> > > <field name="id"
> > > type="java.lang.String">
> > > <bind-xml name="id"
> > > node="element"/>
> > > </field>
> > >
> > > <field name="bar"
> > > type="tudo.foobar.castor.domain.Bar">
> > > <bind-xml name="bar"
> > > node="element"/>
> > > </field>
> > > </class>
> > >
> > > <class name="tudo.foobar.castor.domain.Bar">
> > > <map-to xml="bar"/>
> > >
> > > <field name="greeting"
> > > type="java.lang.String">
> > > <bind-xml name="greeting"
> > > node="element"/>
> > > </field>
> > >
> > > <field name="foo"
> > > type="tudo.foobar.castor.domain.Foo" >
> > > <bind-xml name="foo" />
> > > </field>
> > > </class>
> > >
> > > </mapping>
> > >
> > >
> > > and test it as such:
> > >
> > > private void execute(){
> > > Mapping mapping = new Mapping();
> > >
> > > try {
> > > // 1. Load the mapping information from the file
> > > mapping.loadMapping( "fooMap.xml" );
> > >
> > > // 2. Unmarshal the data
> > > Unmarshaller unmar = new Unmarshaller(mapping);
> > > Foo foo = (Foo)unmar.unmarshal(new InputSource(new
> > > FileReader("FooTest.xml")));
> > >
> > > // 3. Do some processing on the data
> > > System.out.println(foo.getBar().getGreeting());
> > > System.out.println(foo.getBar().getFoo().getId());
> > > } catch (Exception e) {
> > > System.out.println(e);
> > > return;
> > > }
> > > }
> > >
> > >
> > > and the result is:
> > >
> > > hello world
> > > java.lang.NullPointerException
> > >
> > > I have tried setting the location attribute to
> > ".."
> > > but it does not seem to go "backwards". I have
> > also
> > > tried handlers but can only get the current class-
> > not
> > > the parent class.
> > >
> > > Does anyone know how to get a reference to the
> > parent
> > > class of which another class is a member- as just
> > > described????
> > > I tried many sources but have come up with
> > nothing- is
> > > this a Castor limitation?????
> > >
> > > Thaanks,
> >
> === message truncated ===
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Finance: Get your refund fast by filing online.
> http://taxes.yahoo.com/filing.html
>
> -----------------------------------------------------------
> 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