Loading ...
Sorry, an error occurred while loading the content.

Re: Inserting nested XML documents...

Expand Messages
  • mattsns
    Hi! Thanx for the quick answer! It helped me to understand XMLDBMS mapping language better, and now the solution seems obvious... but doesn t work for me!
    Message 1 of 4 , Apr 14, 2004
    • 0 Attachment
      Hi!

      Thanx for the quick answer! It helped me to understand XMLDBMS mapping
      language better, and now the solution seems

      obvious... but doesn't work for me! Actually, it's a bit special :

      As we said, SegmentDecomposition can contain Segment and
      SegmentDecomposition. That's what I want.
      In the database, SegmentDecomposition table contains only one column :
      SegmentDecompositionId (PK).
      So, in the map file, I've declared this row as a primary Key but also
      as a foreign key :

      <Table Name="SegmentDecomposition">
      <Column Name="SegmentDecompositionId" DataType="VARCHAR"
      Nullable="No"/>
      <PrimaryKey>
      <UseColumn Name="SegmentDecompositionId"/>
      </PrimaryKey>
      <ForeignKey Name="FKPointingToPKOfA">
      <UseTable Name="SegmentDecomposition"/>
      <UseUniqueKey Name="PrimaryKey"/>
      <UseColumn Name="SegmentDecompositionId"/>
      </ForeignKey>

      This way, we can know which SegmentDecomposition are inside others.
      And then, I mapped as you said :

      <!-- This RelatedClass says Segment is a complex child of
      SegmentDecomposition -->
      <RelatedClass KeyInParentTable="Unique">
      <ElementType Name="Segment"/>
      <UseUniqueKey Name="PrimaryKey"/>
      <UseForeignKey Name="FK"/>
      </RelatedClass>

      <!-- This RelatedClass says SegmentDecomposition can contain itself as
      a child -->
      <RelatedClass KeyInParentTable="Unique">
      <ElementType Name="SegmentDecomposition" />
      <UseUniqueKey Name="PrimaryKey"/>
      <UseForeignKey Name="FKPointingToPKOfA"/>
      </RelatedClass>

      As I wanted to test this, my program had to retrieve XML, before
      modifying it and reinserting it. But now, the

      retrieveDocument method nevers returns, without any exception thrown.
      Is making a field primary key AND foreign key bad? Really Bad?


      --- In xml-dbms@yahoogroups.com, Ronald Bourret <rpbourret@r...> wrote:
      > According to your map, A (SegmentDecomposition) can contain B (Segment)
      > but can't contain A. This is the reason that the second level A's are
      > not being inserted. (You also show that B (Segment) can contain A
      > (SegmentDecomposition) but your example doesn't show this.)
      >
      > Using A and B (instead of SegmentDecomposition and Segment), you need
      > the following ClassMaps:
      >
      > <ClassMap>
      > <ElementType Name="A" />
      > <ToClassTable Name="A_Table" />
      > ... property maps ...
      >
      > <!-- This RelatedClass says B is a complex child of A -->
      > <RelatedClass KeyInParentTable="Unique">
      > <ElementType Name="B" />
      > <UseUniqueKey Name="PrimaryKey"/>
      > <UseForeignKey Name="FK"/>
      > </RelatedClass>
      >
      > <!-- This RelatedClass says A can contain itself as a child -->
      > <RelatedClass KeyInParentTable="Unique">
      > <ElementType Name="A" />
      > <UseUniqueKey Name="PrimaryKey"/>
      > <UseForeignKey Name="FKPointingToPKOfA"/>
      > </RelatedClass>
      > </ClassMap>
      >
      > <ClassMap>
      > <ElementType Name="B" />
      > <ToClassTable Name="B_Table" />
      > ... property maps ...
      >
      > <!-- RelatedClass for StructuredAnnotation child of B -->
      > <RelatedClass KeyInParentTable="Foreign">
      > <ElementType Name="StructuredAnnotation"/>
      > <UseUniqueKey Name="PrimaryKey"/>
      > <UseForeignKey Name="StructuredAnnotationId"/>
      > </RelatedClass>
      >
      > <!-- It does not appear that any other RelatedClass
      > elements are needed. In particular, it does not
      > appear that A can be a child of B, so no RelatedClass
      > for A is needed here. -->
      > </ClassMap>
      >
      > -- Ron
      >
      > Matts Meus wrote:
      > >
      > > Hi!
      > >
      > > I've got a problem with inserting nested XML elements into database
      > > (using xmldbms V2alpha3 transfer api).
      > > Actually, this is what my XML looks like :
      > >
      > > <A id="1">
      > > <B id="11">...</B>
      > > <B id="12>...</B>
      > > <A id="2">
      > > <B id="21>...</B>
      > > <B id="22">...</B>
      > > </A>
      > > <B id="13">...</B>
      > > <B id="14">...</B>
      > > </A>
      > >
      > > My problem is that when inserting this kind of XML into database, all
      > > "top-level" <A> and <B> elements (1, 11, 12, 13, 14) are correctly
      > > inserted, but all nested <A> and <B> elements (2, 21, 22) aren't.
      > > There's no exception thrown, no error message, everything seems to
      work
      > > fine, except they're not inserted. I tought that mapping an element in
      > > map file makes this element processed every time it occurs in XML
      > > file... Am I missing something? Did I misundertood mapping language?
      > >
      > > Assuming A is SegmentDecomposition and B is Segment, here's my map
      file
      > > :
      > >
      > > <ClassMap>
      > > <ElementType Name="Segment"/>
      > > <ToClassTable Name="Segment"/>
      > > <PropertyMap>
      > > <Attribute Name="id"/>
      > > <ToColumn Name="SegmentId"/>
      > > </PropertyMap>
      > > <PropertyMap>
      > > ...
      > > </PropertyMap>
      > > <RelatedClass KeyInParentTable="Foreign">
      > > <ElementType Name="StructuredAnnotation"/>
      > > <UseUniqueKey Name="PrimaryKey"/>
      > > <UseForeignKey Name="StructuredAnnotationId"/>
      > > </RelatedClass>
      > >
      > > <RelatedClass KeyInParentTable="Unique">
      > > <ElementType Name="SegmentDecomposition"/>
      > > <UseUniqueKey Name="PrimaryKey"/>
      > > <UseForeignKey Name="FK2"/>
      > > </RelatedClass>
      > >
      > > </ClassMap>
      > >
      > > <ClassMap>
      > > <ElementType Name="SegmentDecomposition"/>
      > > <ToClassTable Name="SegmentDecomposition"/>
      > > <PropertyMap>
      > > <Attribute Name="id"/>
      > > <ToColumn Name="SegmentDecompositionId"/>
      > > </PropertyMap>
      > >
      > > <RelatedClass KeyInParentTable="Unique">
      > > <ElementType Name="Segment"/>
      > > <UseUniqueKey Name="PrimaryKey"/>
      > > <UseForeignKey Name="FK"/>
      > > </RelatedClass>
      > >
      > > </ClassMap>
    • Ronald Bourret
      In this case, yes, it is really, really bad :) Suppose, for example, that you have a row in the SegementDecomposition table with an ID of 1. Your map tells
      Message 2 of 4 , Apr 16, 2004
      • 0 Attachment
        In this case, yes, it is really, really bad :)

        Suppose, for example, that you have a row in the SegementDecomposition
        table with an ID of 1. Your map tells XML-DBMS to fetch this row. It
        then tells XML-DBMS to use 1 as a foreign key and fetch the row whose
        primary key is 1. This fetches the same row. And so on, to infinity.

        In spite of the fact that XML-DBMS is in an infinite loop here, I don't
        think this is a bug in XML-DBMS. (Yes, I could check for this, but I
        don't think it's worth the code.)

        The real problem is that I don't understand how this database design can
        work. For example, suppose you have a SegmentDecomposition (SD) whose ID
        is 1, than has a number of Segment (S) children and two SD children,
        whose IDs are 2 and 3. Your tables will look like this:

        SD table:
        ID
        --
        1
        2
        3

        S table:
        SD_ID S_ID other columns
        ----- ---- -------------
        1 1 ... // Parent is SD 1
        1 2 ... // Parent is SD 1
        2 3 ... // Parent is SD 2
        2 4 ... // Parent is SD 2
        3 5 ... // Parent is SD 3
        3 6 ... // Parent is SD 3

        You will note that the S and SD tables are linked by the SD.ID and
        S.SD_ID colums. However, there is no way to link the SD table back to
        itself. Just using the SD.ID column doesn't work, since there is no way
        that rows 2 and 3 can be linked to row 1.

        What you need to do is add another column to the SD table: Parent_ID.
        This is a foreign key that points to SD.ID and allows one row to be
        linked to another. In the example above, this would be:


        SD table:
        ID Parent_ID
        -- ---------
        1 null // SD 1 does not have a parent
        2 1 // The parent of SD 2 is SD 1
        3 1 // The parent of SD 3 is SD 1

        If you then construct the map with SD.ID as a primary key and
        SD.Parent_ID as a foreign key, everything will work as expected.

        -- Ron
        mattsns wrote:
        >
        > Hi!
        >
        > Thanx for the quick answer! It helped me to understand XMLDBMS mapping
        > language better, and now the solution seems
        >
        > obvious... but doesn't work for me! Actually, it's a bit special :
        >
        > As we said, SegmentDecomposition can contain Segment and
        > SegmentDecomposition. That's what I want.
        > In the database, SegmentDecomposition table contains only one column :
        > SegmentDecompositionId (PK).
        > So, in the map file, I've declared this row as a primary Key but also
        > as a foreign key :
        >
        > <Table Name="SegmentDecomposition">
        > <Column Name="SegmentDecompositionId" DataType="VARCHAR"
        > Nullable="No"/>
        > <PrimaryKey>
        > <UseColumn Name="SegmentDecompositionId"/>
        > </PrimaryKey>
        > <ForeignKey Name="FKPointingToPKOfA">
        > <UseTable Name="SegmentDecomposition"/>
        > <UseUniqueKey Name="PrimaryKey"/>
        > <UseColumn Name="SegmentDecompositionId"/>
        > </ForeignKey>
        >
        > This way, we can know which SegmentDecomposition are inside others.
        > And then, I mapped as you said :
        >
        > <!-- This RelatedClass says Segment is a complex child of
        > SegmentDecomposition -->
        > <RelatedClass KeyInParentTable="Unique">
        > <ElementType Name="Segment"/>
        > <UseUniqueKey Name="PrimaryKey"/>
        > <UseForeignKey Name="FK"/>
        > </RelatedClass>
        >
        > <!-- This RelatedClass says SegmentDecomposition can contain itself as
        > a child -->
        > <RelatedClass KeyInParentTable="Unique">
        > <ElementType Name="SegmentDecomposition" />
        > <UseUniqueKey Name="PrimaryKey"/>
        > <UseForeignKey Name="FKPointingToPKOfA"/>
        > </RelatedClass>
        >
        > As I wanted to test this, my program had to retrieve XML, before
        > modifying it and reinserting it. But now, the
        >
        > retrieveDocument method nevers returns, without any exception thrown.
        > Is making a field primary key AND foreign key bad? Really Bad?
      Your message has been successfully submitted and would be delivered to recipients shortly.