Hello,

 

Nobody to help me, please ?

 

Fabien

 

De : Fabien Thouny [mailto:[email protected]] 
Envoyé : vendredi 26 juin 2009 15:36
À : [email protected]
Objet : [castor-user] Problems with use of identity and reference fields

 

Hello,

 

I have some problems to use the "reference" field in the class element.

 

Some more details on a simple project to test and show the problem :

I have a simple class named "ClassA" which has only one attribute "id" like 
this :

 

public class ClassA {

   private int id;

   public final int    getId   ()             { return id; }

   public final void   setId   (final int id) { this.id = id; }

   public       String toString()             {  return ("A : " + getId()); }

}

 

Two others class are referencing the same object ClassA :

public class Test {

   private ClassA classA;

   public  void   setClassA(ClassA classA) { this.classA = classA; }

   public  ClassA getClassA()              { return classA; }

   public  String toString ()              { return ((classA == null)? null  : 
classA.toString());}

}

 

public class TestReference {

   private ClassA classA;

   public  void   setClassA(ClassA classA) { this.classA = classA; }

   public  ClassA getClassA()              { return classA; }

   public  String toString ()              {  return ((classA == null)? null  : 
classA.toString()); }

}

You can notice that these two classes have exactly the same code.

 

Finally there is the main object named "TestConteneur" which references an 
instance of each two previous classes.

public class TestConteneur {

   private Test            test;

   private TestReference   testRef;

   public void setTest(Test test) { this.test = test; }

   public Test getTest() { return test; }

   public void setTestRef(TestReference testRef) { this.testRef = testRef; }

   public TestReference getTestRef() { return testRef; }

   public String toString() { return (test + "\nref : " + testRef); }

}

 

Now the mapping file :

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" 

      "http://castor.exolab.org/mapping.dtd";>

<mapping>

      <description>Test mapping</description>

      

      <class name="test.ClassA" identity="id">

            <field name="id" type="integer">

                  <bind-xml name="id" node="attribute"/>

            </field>

      </class>

      <class name="test.TestReference">

            <field name="classA" 

                  type="test.ClassA">

                  <bind-xml name="ARef" reference="true"/>

            </field>

      </class>

      <class name="test.Test">

            <field name="classA" 

                  type="test.ClassA">

                  <bind-xml name="A"/>

            </field>

      </class>

      <class name="test.TestConteneur">

            <map-to xml="Conteneur"/>

            <field name="test" 

                  type="test.Test">

                  <bind-xml name="Test"/>

            </field>

            <field name="testRef" 

                  type="test.TestReference">

                  <bind-xml name="TestRef"/>

            </field>

      </class>

</mapping>

 

You can notice that I want a reference on the object classA in the class 
TestReference.

 

So when I marshal a TestConteneur object, I got this :

<?xml version="1.0" encoding="UTF-8"?>

<Conteneur>

    <Test>

        <A id="1"/>

    </Test>

    <TestRef>

        <ARef>1</ARef>

    </TestRef>

</Conteneur>

 

Fine !

 

But when I want to unmarshall, (with the same mapping file, I use XMLContext to 
create my marshallers and unmarshallers ;-))

 

I have an error :

org.exolab.castor.xml.MarshalException: The following exception occured while 
validating field: testRef of class: test.TestConteneur: The object associated 
with IDREF "A : 1" of type class test.ClassA has no ID!{File: [not available]; 
line: 9; column: 13}

      at 
org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unmarshaller.java:794)

      at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:760)

      at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:626)

      at test.TestMain.main(TestMain.java:77)

 

I don't know why, but it seems that the id of the ClassA object is not 
correctly treated.

 

What did I forget ? Can you help me,  please?

 

Thanks,

 

Fabien

 

P.S : my version of Castor is the 1.3.

P.P.S. : sorry for my poor and bad English.

Reply via email to