Hi Werner

I have sent you the student, teacher and course classes. student and
teacher extends person class, which is also included.
Plus the test class which marshalls the student.

Don't get confused about the different id variables in the classes.
I started to experiment ....... !! :-(
I started with the property id, which is of type Integer. But I read that
ID/IDREF in XML syntax are not allowed to be of integer type. (ID should
be treated as identifiers similar to progamming language identifiers.)
Besides, each ID/IDREF in XML should identify a node uniquely.
With this in mind I added the property objectId which would prefix a
string before the id value to make object-id a global identifier in XML
representation.
This did not solve my problem.....
Then I introduced the property ID (uppercase). At this point I had become
totally confused in the process of making the marshalling of student to
work.

That's the reason for all the code mess up.
In all cases, I was not able to get the correct marshalling of student.

I hope you can manage to reproduce this problem and hopefully give a
solution.
I can't imagine I am the first person with this problem. I sought for
similar cases on the web, but did not find anything.


package acme;

import java.util.List;

/**
 *
 */

/**
 * @author UWieske
 * @castor.class xml="student" extends="acme.Person"
 */
public class Student extends Person {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        private List<Course> courses;

        private Boolean certified;


        /**
         * @castor.field type="boolean"
         * @castor.field-xml name="is-certified"
         * @return
         */
        public Boolean getCertified() {
                return certified;
        }

        public void setCertified(Boolean certified) {
                this.certified = certified;
        }

        /**
         * @castor.field collection="arraylist" type="acme.Course"
         * @castor.field-xml name="course" location="courses"
         *
         * @return
         */
        public List<Course> getCourses() {
                return courses;
        }

        public void setCourses(List<Course> courses) {
                this.courses = courses;
        }



        public String getObjectId() {
                return "student-"+ this.getId();
        }


        public void setObjectId(String objectId) {
                setId(new 
Integer(objectId.substring(objectId.lastIndexOf('-'))));
        }

}







package acme;

import java.io.Serializable;

/**
 * @author UWieske
 * @castor.class xml="person"   identity="objectId"
 */
public class Person implements Serializable {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        private Integer id;

        private String firstname;

        private String lastname;

        private String ID;

        public Person() {

        }

        public Person(Integer id, String fname, String lname) {
                this.id = id;
                this.firstname = fname;
                this.lastname = lname;
        }

        /**
         * @castor.field
         * @castor.field-xml
         * @return
         */
        public String getFirstname() {
                return firstname;
        }

        public void setFirstname(String firstname) {
                this.firstname = firstname;
        }

        /**
         * @castor.field
         * @castor.field-xml name="id" node="attribute"
         * @return
         */
        public Integer getId() {
                return id;
        }

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

        /**
         * @castor.field
         * @castor.field-xml
         * @return
         */
        public String getLastname() {
                return lastname;
        }


        public void setLastname(String lastname) {
                this.lastname = lastname;
        }

        /**
         * @castor.field
         * @castor.field-xml name="object-id" node="attribute"
         * @return
         */
        public String getObjectId() {
                return "person-"+ id;
        }

        /**
         *
         * @return
         */
        public String getID() {
                return ID;
        }

        public void setID(String id) {
                ID = id;
        }

}





package acme;

import java.util.List;

/**
 * @castor.class  xml="teacher"  extends="acme.Person"
 * @author UWieske
 *
 */
public class Teacher extends Person {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        private List<Course> courses;
        private List<Student> pupils;


        public List<Course> getCourses() {
                return courses;
        }

        public void setCourses(List<Course> courses) {
                this.courses = courses;
        }

        /**
         * @castor.field collection="arraylist" type="acme.Student"
         * @castor.field-xml name="pupil"   location="pupils" reference="false"
         *
         *
         * @return
         */
        public List<Student> getPupils() {
                return pupils;
        }

        public void setPupils(List<Student> pupils) {
                this.pupils = pupils;
        }



        public String getObjectId() {
                return "teacher-"+ this.getId();
        }


}



package acme;

import java.io.Serializable;
import java.util.List;

/**
 * @castor.class xml="course" identity="objectId"
 * @author UWieske
 *
 */
public class Course implements Serializable {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        private Integer id;

        private String code;

        private String name;

        private List<Teacher> teachers;

        /**
         * @castor.field
         * @castor.field-xml
         * @return
         */
        public String getCode() {
                return code;
        }

        public void setCode(String code) {
                this.code = code;
        }

        /**
         * @castor.field
         * @castor.field-xml node="attribute"
         * @return
         */
        public int getId() {
                return id;
        }

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

        /**
         * @castor.field
         * @castor.field-xml
         * @return
         */
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        /**
         * @castor.field collection="arraylist" type="acme.Teacher"
         * @castor.field-xml name="teacher"  location="teachers"
         * @return
         */
        public List<Teacher> getTeachers() {
                return teachers;
        }

        public void setTeachers(List<Teacher> teachers) {
                this.teachers = teachers;
        }


        /**
         * @castor.field
         * @castor.field-xml name="object-id" node="attribute"
         * @return
         */
        public String getObjectId() {
                return "course-"+ this.getId();
        }



}


package acme;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;

import junit.framework.TestCase;

/**
 * @author UWieske
 *
 */
public class MarahallerTest extends TestCase {

        /* (non-Javadoc)
         * @see junit.framework.TestCase#setUp()
         */
        protected void setUp() throws Exception {
                super.setUp();
        }

        /* (non-Javadoc)
         * @see junit.framework.TestCase#tearDown()
         */
        protected void tearDown() throws Exception {
                super.tearDown();
        }

        public void testMarshall() {

                Student stud = createStudent();

                // Load Mapping
                Mapping mapping = new Mapping();

                 //Create a File to marshal to
                Writer writer;
                try {

                        mapping.loadMapping("target/xdoclet/mapping.xml");
                        writer = new FileWriter("test.xml");
                        Marshaller marshaller = new Marshaller(writer);
                        marshaller.setMapping(mapping);


                        marshaller.marshal(stud);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (MarshalException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ValidationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (MappingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }


        }




        private Student createStudent() {


                Student s1 = new Student();

                //Student 1
                s1.setId(1);
                s1.setID("STUDENT-1");
                s1.setLastname("Doe");
                s1.setFirstname("John");
                s1.setCertified(true);

                //Student 1 followes two courses, i.e. course c1 and c2
                ArrayList<Course> courses = new ArrayList<Course>();
                Course c1 = new Course();
                c1.setCode("EN123");
                c1.setName("English");
                c1.setId(3);
                courses.add(c1);

                Course c2 = new Course();
                c2.setCode("SP111");
                c2.setName("Spanish");
                c2.setId(new Integer(232)) ;
                courses.add(c2);

                s1.setCourses(courses);


                // Course 1 is being given by teacher 1
                ArrayList<Teacher> teachersC1 = new ArrayList<Teacher>();
                Teacher t1 = new Teacher();
                t1.setId(34);
                t1.setID("TEACHER-34");
                t1.setLastname("Rodman");
                t1.setFirstname("Dennis");
                teachersC1.add(t1);
                c1.setTeachers(teachersC1);

                // Teacher t1 is responsible for student s2
                ArrayList<Student> pupilsT1 = new ArrayList<Student>();
                Student s2 = new Student();
                s2.setId(2);
                s2.setID("STUDENT-2");
                s2.setLastname("Jacky");
                s2.setFirstname("Onasis");
                s2.setCertified(false);
                pupilsT1.add(s2);
                t1.setPupils(pupilsT1);


                // Course 2 is being given by teachers 2 and 3
                ArrayList<Teacher> teachersC2 = new ArrayList<Teacher>();
                Teacher t2 = new Teacher();
                t2.setId(324);
                t2.setID("TEACHER-324");
                t2.setLastname("Newman");
                t2.setFirstname("Paul");
                teachersC2.add(t2);

                Teacher t3 = new Teacher();
                t3.setId(315);
                t3.setID("TEACHER-315");
                t3.setLastname("Clinton");
                t3.setFirstname("Bill");
                teachersC2.add(t3);
                c2.setTeachers(teachersC2);

                // Teacher t3 is responsible for students 1, 2 and 3
                ArrayList<Student> pupilsT3 = new ArrayList<Student>();
                pupilsT3.add(s1); // THIS STUDENT IS NOT MARSHALLED??!
                pupilsT3.add(s2);

                Student s3 = new Student();
                s3.setId(55);
                s3.setID("STUDENT-55");
                s3.setLastname("Jacky");
                s3.setFirstname("Onasis");
                s3.setCertified(false);
                pupilsT3.add(s3);

                t3.setPupils(pupilsT3);


                return s1;

        }


}







Kind Regards

Urso

" Werner Guttmann"
> Urso,
>
> I need the Student class (as well as any classes referenced from it) as
> well.
>
> Thanks
> Werner
>
>
> ________________________________
>
>       From: Urso Wieske [mailto:[EMAIL PROTECTED]
>       Sent: Mittwoch, 24. Jänner 2007 14:59
>       To: [email protected]
>       Subject: RE: [castor-user] Marshalling Non-Hierarchical object models
>
>
>
>       Hi Werner,
>
>
>
>       Check my quote in comments in the XML file: "<!-- ? WHERE IS my pupil 1
> (student 1) John Doe ???????? -->".......
>
>       What's the problem here? I should have had an entry of John Doe at that
> point.
>
>
>
>       Kind regards
>
>       Urso
>
>
>
>
>
>       My marshalled XML file looks like this:
>
>
>
>       <?xml version="1.0" encoding="UTF-8"?>
>
>       <student id="1" object-id="student-1">
>
>           <firstname>John</firstname>
>
>           <lastname>Doe</lastname>
>
>           <is-certified>true</is-certified>
>
>           <courses>
>
>               <course id="3" object-id="course-3">
>
>                   <code>EN123</code>
>
>                   <name>English</name>
>
>                   <teachers>
>
>                       <teacher id="34" object-id="teacher-34">
>
>                           <firstname>Dennis</firstname>
>
>                           <lastname>Rodman</lastname>
>
>                           <pupils>
>
>                               <pupil id="2" object-id="student-2">
>
>                                   <firstname>Onasis</firstname>
>
>                                   <lastname>Jacky</lastname>
>
>                                   <is-certified>false</is-certified>
>
>                                   <courses/>
>
>                               </pupil>
>
>                           </pupils>
>
>                       </teacher>
>
>                   </teachers>
>
>               </course>
>
>               <course id="232" object-id="course-232">
>
>                   <code>SP111</code>
>
>                   <name>Spanish</name>
>
>                   <teachers>
>
>                       <teacher id="324" object-id="teacher-324">
>
>                           <firstname>Paul</firstname>
>
>                           <lastname>Newman</lastname>
>
>                           <pupils/>
>
>                       </teacher>
>
>                       <teacher id="315" object-id="teacher-315">
>
>                           <firstname>Bill</firstname>
>
>                           <lastname>Clinton</lastname>
>
>                           <pupils>
>
>                               <!-- ? WHERE IS my pupil 1 (student 1) John Doe
> ???????? -->
>
>
>
>                               <pupil id="2" object-id="student-2">
>
>                                   <firstname>Onasis</firstname>
>
>                                   <lastname>Jacky</lastname>
>
>                                   <is-certified>false</is-certified>
>
>                                   <courses/>
>
>                               </pupil>
>
>                               <pupil id="55" object-id="student-55">
>
>                                   <firstname>Onasis</firstname>
>
>                                   <lastname>Jacky</lastname>
>
>                                   <is-certified>false</is-certified>
>
>                                   <courses/>
>
>                               </pupil>
>
>                           </pupils>
>
>                       </teacher>
>
>                   </teachers>
>
>               </course>
>
>           </courses>
>
>       </student>
>
>
>
>       MAPPING:
>
>       My mapping looks like this:
>
>
>
>
>
>
>
>       <?xml version="1.0" encoding="UTF-8"?>
>
>       <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
> "http://castor.codehaus.org/mapping.dtd";>
>
>
>
>       <!--Generated file. Do not edit.-->
>
>       <mapping>
>
>         <class identity="objectId" name="acme.School">
>
>           <map-to xml="school"/>
>
>           <field name="students" collection="arraylist" type="acme.Student">
>
>             <bind-xml name="student" location="students"/>
>
>           </field>
>
>         </class>
>
>         <class identity="objectId" name="acme.Person">
>
>           <map-to xml="person"/>
>
>           <field name="firstname" type="string">
>
>             <bind-xml/>
>
>           </field>
>
>           <field name="id" type="java.lang.Integer">
>
>             <bind-xml name="id" node="attribute"/>
>
>           </field>
>
>           <field name="lastname" type="string">
>
>             <bind-xml/>
>
>           </field>
>
>           <field name="objectId" type="string">
>
>             <bind-xml name="object-id" node="attribute"/>
>
>           </field>
>
>         </class>
>
>         <class extends="acme.Person" name="acme.Teacher">
>
>           <map-to xml="teacher"/>
>
>           <field name="pupils" collection="arraylist" type="acme.Student">
>
>             <bind-xml name="pupil" location="pupils" reference="false"/>
>
>           </field>
>
>         </class>
>
>         <class extends="acme.Person" name="acme.Student">
>
>           <map-to xml="student"/>
>
>           <field name="certified" type="boolean">
>
>             <bind-xml name="is-certified"/>
>
>           </field>
>
>           <field name="courses" collection="arraylist" type="acme.Course">
>
>             <bind-xml name="course" location="courses"/>
>
>           </field>
>
>         </class>
>
>         <class identity="objectId" name="acme.Course">
>
>           <map-to xml="course"/>
>
>           <field name="code" type="string">
>
>             <bind-xml/>
>
>           </field>
>
>           <field name="id" type="integer">
>
>             <bind-xml node="attribute"/>
>
>           </field>
>
>           <field name="name" type="string">
>
>             <bind-xml/>
>
>           </field>
>
>           <field name="teachers" collection="arraylist" type="acme.Teacher">
>
>             <bind-xml name="teacher" location="teachers"/>
>
>           </field>
>
>           <field name="objectId" type="string">
>
>             <bind-xml name="object-id" node="attribute"/>
>
>           </field>
>
>         </class>
>
>         <!--start merging from source: <undefined merge
> dir>/class-mappings.xml-->
>
>         <!--to specify aditional class mappings put file "class-mappings.xml"
> into
>
>                       merge directory.
>
>                       Define root element as <j:jelly  
> xmlns:j="jelly:core">-->
>
>         <!--end merging from source: <undefined merge
> dir>/class-mappings.xml-->
>
>       </mapping>
>
>
>
>
>
>       My test function from my TestCase: (part of it)
>
>
>
>       public void testMarshall() {
>
>
>
>                               Student stud = createStudent();
>
>
>
>                               // Load Mapping
>
>                               Mapping mapping = new Mapping();
>
>
>
>                                //Create a File to marshal to
>
>                               Writer writer;
>
>                               try {
>
>
>
>                                          
> mapping.loadMapping("target/xdoclet/mapping.xml");
>
>                                          writer = new FileWriter("test.xml");
>
>                                          Marshaller marshaller = new
> Marshaller(writer);
>
>                                          marshaller.setMapping(mapping);
>
>
>
>
>
>                                          marshaller.marshal(stud);
>
>                               } catch (IOException e) {
>
>                                          // TODO Auto-generated catch block
>
>                                          e.printStackTrace();
>
>                               } catch (MarshalException e) {
>
>                                          // TODO Auto-generated catch block
>
>                                          e.printStackTrace();
>
>                               } catch (ValidationException e) {
>
>                                          // TODO Auto-generated catch block
>
>                                          e.printStackTrace();
>
>                               } catch (MappingException e) {
>
>                                          // TODO Auto-generated catch block
>
>                                          e.printStackTrace();
>
>                               }
>
>
>
>
>
>                   }
>
>
>
>
>
>
>
>
>
>                   private Student createStudent() {
>
>
>
>
>
>                               Student s1 = new Student();
>
>
>
>                               //Student 1
>
>                               s1.setId(1);
>
>                               s1.setID("STUDENT-1");
>
>                               s1.setLastname("Doe");
>
>                               s1.setFirstname("John");
>
>                               s1.setCertified(true);
>
>
>
>                               //Student 1 followes two courses, i.e. course c1
> and c2
>
>                               ArrayList<Course> courses = new
> ArrayList<Course>();
>
>                               Course c1 = new Course();
>
>                               c1.setCode("EN123");
>
>                               c1.setName("English");
>
>                               c1.setId(3);
>
>                               courses.add(c1);
>
>
>
>                               Course c2 = new Course();
>
>                               c2.setCode("SP111");
>
>                               c2.setName("Spanish");
>
>                               c2.setId(new Integer(232)) ;
>
>                               courses.add(c2);
>
>
>
>                               s1.setCourses(courses);
>
>
>
>
>
>                               // Course 1 is being given by teacher 1
>
>                               ArrayList<Teacher> teachersC1 = new
> ArrayList<Teacher>();
>
>                               Teacher t1 = new Teacher();
>
>                               t1.setId(34);
>
>                               t1.setID("TEACHER-34");
>
>                               t1.setLastname("Rodman");
>
>                               t1.setFirstname("Dennis");
>
>                               teachersC1.add(t1);
>
>                               c1.setTeachers(teachersC1);
>
>
>
>                               // Teacher t1 is responsible for student s2
>
>                               ArrayList<Student> pupilsT1 = new
> ArrayList<Student>();
>
>                               Student s2 = new Student();
>
>                               s2.setId(2);
>
>                               s2.setID("STUDENT-2");
>
>                               s2.setLastname("Jacky");
>
>                               s2.setFirstname("Onasis");
>
>                               s2.setCertified(false);
>
>                               pupilsT1.add(s2);
>
>                               t1.setPupils(pupilsT1);
>
>
>
>
>
>                               // Course 2 is being given by teachers 2 and 3
>
>                               ArrayList<Teacher> teachersC2 = new
> ArrayList<Teacher>();
>
>                               Teacher t2 = new Teacher();
>
>                               t2.setId(324);
>
>                               t2.setID("TEACHER-324");
>
>                               t2.setLastname("Newman");
>
>                               t2.setFirstname("Paul");
>
>                               teachersC2.add(t2);
>
>
>
>                               Teacher t3 = new Teacher();
>
>                               t3.setId(315);
>
>                               t3.setID("TEACHER-315");
>
>                               t3.setLastname("Clinton");
>
>                               t3.setFirstname("Bill");
>
>                               teachersC2.add(t3);
>
>                               c2.setTeachers(teachersC2);
>
>
>
>                               // Teacher t3 is responsible for students 1, 2
> and 3
>
>                               ArrayList<Student> pupilsT3 = new
> ArrayList<Student>();
>
>                               pupilsT3.add(s1); // THIS STUDENT IS NOT
> MARSHALLED??!
>
>                               pupilsT3.add(s2);
>
>
>
>                               Student s3 = new Student();
>
>                               s3.setId(55);
>
>                               s3.setID("STUDENT-55");
>
>                               s3.setLastname("Jacky");
>
>                               s3.setFirstname("Onasis");
>
>                               s3.setCertified(false);
>
>                               pupilsT3.add(s3);
>
>
>
>                               t3.setPupils(pupilsT3);
>
>
>
>
>
>                               return s1;
>
>
>
>                   }
>
>
>
>
>
>
>
>
>
>
> ________________________________
>
>
>       Van: Werner Guttmann [mailto:[EMAIL PROTECTED]
>       Verzonden: dinsdag 23 januari 2007 11:49
>       Aan: [email protected]
>       Onderwerp: RE: [castor-user] Marshalling Non-Hierarchical object models
>
>
>
>       Urso,
>
>
>
>       without being able to 'peek' at the xml file, domain objects, etc, in
> detail, i guess we cannot be of any help to you.
>
>
>
>       Werner
>
>
>
>
> ________________________________
>
>
>               From: Urso Wieske [mailto:[EMAIL PROTECTED]
>               Sent: Montag, 22. Jänner 2007 18:02
>               To: [email protected]
>               Subject: [castor-user] Marshalling Non-Hierarchical object 
> models
>
>               Hi,
>
>
>
>               I am trying to marshall my object model to XML.
>
>               I have the following problem within my object graph:
>
>
>
>
>
>               ROOT: student1 -> {course1, course2, course3}
>
>               course1-> {student1, student2}
>
>
>
>               The above means "student1 has three courses (1,2,3) and course1 
> contains
> is attended by two students (1,2).
>
>               Notice the student1 in the collection of course1 is actually a 
> reference
> back to the root element student1.
>
>
>
>               When I marshall the above structure, I expect a reference in the
> collection {student1,student2} of course1.
>
>
>
>               The thing is: ONLY student2 is present in the collection of 
> course1 !
> (Student1 has disappeared !...?? )
>
>
>
>               I checked with the Castor HOW-TOs with respect to the usage of
> references.
>
>               I declared identity attributes on all classes in my mapping. I 
> declared
> the attribute reference to true in the bind-xml tags...
>
>               I think I followed all steps properly.
>
>
>
>               But this seems not to work?
>
>               Can someone point me to the problem and solution?
>
>
>
>
>
>               Kind regards
>
>               Urso
>
>



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to