Skip to content
Alexey Valikov edited this page Aug 31, 2017 · 1 revision

Mapping groups

Assume you have a group which is used in several complex items:

    <xsd:group name="HJIII-91-Group">
        <xsd:sequence>
            <xsd:element name="one" type="xsd:string" minOccurs="0"/>
            <xsd:element name="two" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
    </xsd:group>

    <xsd:complexType name="HJIII-91-A-Type">
        <xsd:sequence>
            <xsd:group ref="test:HJIII-91-Group"/>
            <xsd:element name="athree" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string" ... />
    </xsd:complexType>

    <xsd:complexType name="HJIII-91-B-Type">
        <xsd:sequence>
            <xsd:group ref="test:HJIII-91-Group"/>
            <xsd:element name="bthree" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string" ... />
    </xsd:complexType>

You may want to map elements of this group to one table.

You can achieve this using secondary table customization:

    <xsd:complexType name="HJIII-91-A-Type">
        <xsd:annotation>
            <xsd:appinfo>
                <hj:entity>
                    <orm:table name="HJIII91GROUP"/>
                    <orm:secondary-table name="HJIII91ATYPE"/>
                </hj:entity>
            </xsd:appinfo>
        </xsd:annotation>
        <xsd:sequence>
            <xsd:group ref="test:HJIII-91-Group"/>
            <xsd:element name="athree" type="xsd:string" minOccurs="0">
                <xsd:annotation>
                    <xsd:appinfo>
                        <hj:basic>
                            <orm:column table="HJIII91ATYPE"/>
                        </hj:basic>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string">
            <xsd:annotation>
                <xsd:appinfo>
                    <hj:id/>
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>

Now elements from the group (one, two) are mapped to the group table, elements of the complex type (athree) are mapped to the type table; type table references group table:

create table HJIII91ATYPE (ATHREE varchar(255), ID varchar(255) not null, primary key (ID))
create table HJIII91GROUP (ID varchar(255) not null, ONE varchar(255), TWO varchar(255), primary key (ID))
alter table HJIII91ATYPE add constraint FKE2590A1CF58BF275 foreign key (ID) references HJIII91GROUP

Sample XML:

<HJIII-91-A xmlns="urn:test" id="a">
    <one>one</one>
    <two>two</two>
    <athree>three</athree>
</HJIII-91-A>

Results in the following SQL:

INSERT INTO HJIII91GROUP VALUES('a','one','two')
INSERT INTO HJIII91ATYPE VALUES('three','a')

This approach is a bit limited (ex. you can use it with one group only, ids must be the same). It also feels a bit weird to use group table as primary table and type table as secondary - I would expect it to be the other way round. But I could not find a way to reverse the constraint direction at the moment.

Clone this wiki locally