Keith,
Thanks for the lead- I believe that is the right
trail- however, when I implement the ID/IDREF
attributes- I still do not get the reference to the
parent from the child - note my differences from your
example:
1. The Parent Foo has the child Bar class as an
attribute: Foo has Bar bar; as a class member.
2. It is not necesarily the case that Bar extends Foo
(am not certain if that is what was being understood)-
Bar should just be a child class attribute with a
reference to the parent- no special relation outside
of that.
3. It is possible that Foo may have more than 1 field
as "ID" or primary -key.
4. The parent reference is a type node="element"
rather than "attribute"- although I get the same
result even if I do switch that:
Nevertheless- when I use the following mapping:
<class name="tudo.foobar.castor.domain.Foo"
identity="id">
<map-to xml="foo"/>
<field name="id"
type="java.lang.String" >
<bind-xml name="id"
node="element"/>
</field>
<field name="bar" type="Bar">
<bind-xml name="bar"
node="element"/>
</field>
</class>
<class name="Bar" >
<map-to xml="bar"/>
<field name="greeting"
type="java.lang.String">
<bind-xml name="greeting"
node="element"/>
</field>
<field name="parent" type="Foo" >
<bind-xml name="foo"
reference="true" />
</field>
</class>
with the classes:
public class Foo {
public Bar bar;
public String id;
and
public class Bar{
public String greeting;
public Foo parent;
and xml:
<foo>
<id>42</id>
<bar>
<greeting>hello world</greeting>
</bar>
</foo>
and run the test with:
System.out.println(foo.getBar().getGreeting());
System.out.println(foo.getBar().getParent().getId());
I would expect:
hello world
42
Instead I still get:
hello world
java.lang.NullPointerException
The Child Bar class does not seem to get a reference
to the parent Foo even using the ID and IDREF
tags....although I may very well be mis-applying them.
Any further thoughts/corrections would be most
welcome-
Thanks again for the help- I have been racking my
brains on this for quite awhile....
Jim
--- Keith Visco <[EMAIL PROTECTED]> wrote:
>
>
> 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"/>
> > > >
>
=== 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