13.  XML Schema Format

The schema tool and schema factories all use the same XML format to represent database schema. The Document Type Definition (DTD) for schema information is presented below, followed by examples of schema definitions in XML.

<!ELEMENT schemas (schema)+>
<!ELEMENT schema (table|sequence)+>
<!ATTLIST schema name CDATA #IMPLIED>

<!ELEMENT sequence EMPTY>
<!ATTLIST sequence name CDATA #REQUIRED>
<!ATTLIST sequence initial-value CDATA #IMPLIED>
<!ATTLIST sequence increment CDATA #IMPLIED>
<!ATTLIST sequence allocate CDATA #IMPLIED>

<!ELEMENT table (column|index|pk|fk|unique)+>
<!ATTLIST table name CDATA #REQUIRED>

<!ELEMENT column EMPTY>
<!ATTLIST column name CDATA #REQUIRED> 
<!ATTLIST column type (array | bigint | binary | bit | blob | char | clob 
    | date | decimal | distinct | double | float | integer | java_object 
    | longvarbinary | longvarchar | null | numeric | other | real | ref 
    | smallint | struct | time | timestamp | tinyint | varbinary | varchar) 
    #REQUIRED>
<!ATTLIST column not-null (true|false) "false">
<!ATTLIST column auto-assign (true|false) "false">
<!ATTLIST column default CDATA #IMPLIED>
<!ATTLIST column size CDATA #IMPLIED>
<!ATTLIST column decimal-digits CDATA #IMPLIED>

<!-- the type-name attribute can be used when you want OpenJPA to   -->
<!-- use a particular SQL type declaration when creating the     -->
<!-- column. It is up to you to ensure that this type is         -->
<!-- compatible with the JDBC type used in the type attribute.   -->
<!ATTLIST column type-name CDATA #IMPLIED>

<!-- the 'column' attribute of indexes, pks, and fks can be used -->
<!-- when the element has only one column (or for foreign keys,  -->
<!-- only one local column); in these cases the on/join child    -->
<!-- elements can be omitted                                     -->
<!ELEMENT index (on)*>
<!ATTLIST index name CDATA #REQUIRED>
<!ATTLIST index column CDATA #IMPLIED>
<!ATTLIST index unique (true|false) "false">

<!-- the 'logical' attribute of pks should be set to 'true' if   --> 
<!-- the primary key does not actually exist in the database,    --> 
<!-- but the given column should be used as a primary key for    -->
<!-- O-R purposes                                                -->
<!ELEMENT pk (on)*>
<!ATTLIST pk name CDATA #IMPLIED>
<!ATTLIST pk column CDATA #IMPLIED>
<!ATTLIST pk logical (true|false) "false">

<!ELEMENT on EMPTY>
<!ATTLIST on column CDATA #REQUIRED>

<!-- fks with a delete-action of 'none' are similar to logical   -->
<!-- pks; they do not actually exist in the database, but        -->
<!-- represent a logical relation between tables (or their       -->
<!-- corresponding Java classes)                                 -->
<!ELEMENT fk (join)*>
<!ATTLIST fk name CDATA #IMPLIED>
<!ATTLIST fk deferred (true|false) "false">
<!ATTLIST fk to-table CDATA #REQUIRED>
<!ATTLIST fk column CDATA #IMPLIED>
<!ATTLIST fk delete-action (cascade|default|exception|none|null) "none">

<!ELEMENT join EMPTY>
<!ATTLIST join column CDATA #REQUIRED>
<!ATTLIST join to-column CDATA #REQUIRED>
<!ATTLIST join value CDATA #IMPLIED>

<!-- unique constraint -->
<!ELEMENT unique (on)*>
<!ATTLIST unique name CDATA #IMPLIED>
<!ATTLIST unique column CDATA #IMPLIED>
<!ATTLIST unique deferred (true|false) "false">

Example 4.19.  Basic Schema

A very basic schema definition.

<schemas>
    <schema>
        <sequence name="S_ARTS"/>
        <table name="ARTICLE">
            <column name="TITLE" type="varchar" size="255" not-null="true"/>
            <column name="AUTHOR_FNAME" type="varchar" size="28"> 
            <column name="AUTHOR_LNAME" type="varchar" size="28"> 
            <column name="CONTENT" type="clob"> 
        </table>
        <table name="AUTHOR">
            <column name="FIRST_NAME" type="varchar" size="28" not-null="true"> 
            <column name="LAST_NAME" type="varchar" size="28" not-null="true"> 
        </table>
    </schema>
</schemas>

Example 4.20.  Full Schema

Expansion of the above schema with primary keys, constraints, and indexes, some of which span multiple columns.

<schemas>
    <schema>
        <sequence name="S_ARTS"/>
        <table name="ARTICLE">
            <column name="TITLE" type="varchar" size="255" not-null="true"/>
            <column name="AUTHOR_FNAME" type="varchar" size="28"> 
            <column name="AUTHOR_LNAME" type="varchar" size="28"> 
            <column name="CONTENT" type="clob"> 
            <pk column="TITLE"/>
            <fk to-table="AUTHOR" delete-action="exception">
                <join column="AUTHOR_FNAME" to-column="FIRST_NAME"/>
                <join column="AUTHOR_LNAME" to-column="LAST_NAME"/>
            </fk>
            <index name="ARTICLE_AUTHOR">
                <on column="AUTHOR_FNAME"/>
                <on column="AUTHOR_LNAME"/>
            </index>
        </table>
        <table name="AUTHOR">
            <column name="FIRST_NAME" type="varchar" size="28" not-null="true"> 
            <column name="LAST_NAME" type="varchar" size="28" not-null="true"> 
            <pk>
                <on column="FIRST_NAME"/>
                <on column="LAST_NAME"/>
            </pk>
        </table>
    </schema>
</schemas>