This one time, at band camp, Joseph George-AGJ020 said:

JG>I am using Castor JDO in my current project.  I am trying to find an example
JG>of how to store  a super and sub class into the same one table in the db.
JG> 
JG>I.E  STUDENT and PROF both extend PERSON
JG> 
JG>PERSON    
JG>    String name
JG>    String ssn
JG> 
JG>STUDENT
JG>    String gpa
JG>    Collection Courses
JG> 
JG>PROF
JG>    boolean tenured 
JG>    String FacultyName
JG> 
JG>I want have one table called PERSON in the DB that would contain all fields
JG>in PERSON and those found in PROF and STUDENT plus an extra field in the
JG>table and XML called PersonType to distinguish between PROF and STUDENT.
JG>I've been looking for a mapping.xml file code snippet that would show how to
JG>do this.
JG> 
JG>Does Castor provide a mechanism for doing this or is there extra code I need
JG>to write to handle this?
JG> 
JG>I found how to write the XML file so that each table has its own table but
JG>want to store all three classes using one table.
JG> 
JG>If PERSON class is abstract does it make a diff? 

George,

Castor will handle the persistence of these objects but it's up to you
to provide the mapping for the objects into the database. Each object
would share the same identity field and each object would map to the
PERSON table. What kind of inheritance do these objects possess?

This scenario is just like mapping different views of the same object
only with multiple objects:

    http://www.castor.org/tips-tricks.html#JDO:-Views-of-Same-Object

Below is a quick example mapping just off the top of my head:

    <mapping>
        <class name="Person" identity="id">
        <map-to table="person" />
            <field name="id" type="long">
                <sql name="id" type="long" />
            </field>
            <field name="name" type="string">
                <sql name="name" type="varchar" />
            </field>
            <field name="ssn" type="int">
                <sql name="ssn" type="int" />
            </field>
        </class>

        <class name="Student" identity="id">
        <map-to table="person" />
            <field name="id" type="long">
                <sql name="id" type="long" />
            </field>
            <field name="name" type="string">
                <sql name="name" type="varchar" />
            </field>
            <field name="ssn" type="int">
                <sql name="ssn" type="int" />
            </field>
            <field name="gpa" type="int">
                <sql name="gpa" type="int" />
            </field>
            <field name="courses" type="Course" collection="collection">
                <sql name="course_id" many-table="course" many-key="student_id" />
            </field>
        </class>

        <class name="Professor" identity="id">
        <map-to table="person" />
            <field name="id" type="long">
                <sql name="id" type="long" />
            </field>
            <field name="name" type="string">
                <sql name="name" type="varchar" />
            </field>
            <field name="ssn" type="int">
                <sql name="ssn" type="int" />
            </field>
            <field name="tenured" type="boolean">
                <sql name="tenured" type="bit" />
            </field>
            <field name="factultyName" type="string">
                <sql name="faculty_name" type="varchar" />
            </field>
        </class>

        <class name="Course" identity="id">
        <map-to table="course" />
            ...
        </class>
    <mapping>

The example above is incomplete and does not include any kind of
inheritance. If there is inheritance, I hazard a guess that the mapping
would not change much (if at all) because a Student object contains
fields that a Professor does not. Also, use of the 'extends' attribute
in the <class> element wouldn't work correctly because all objects are
mapped to the same table. My thought in providing the example is that
it might help you get moving in the right direction.

If the Person object is abstrct, it would not be persisted per se. IOW,
there would be no object mapping for the Person object, only mappings
for the Student and the Professor because those are the concrete impls
of the Person. By way of persisting a Student or a Professor, the fields
from the Person object would be persisted.

Bruce
-- 
perl -e 'print unpack("u30","<0G)[EMAIL PROTECTED]&5R\"F9E<G)E=\$\!F<FEI+F-O;0\`\`");'

The Castor Project 
http://www.castor.org/

Apache Geronimo 
http://incubator.apache.org/projects/geronimo.html

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to