Hyperjaxb/JPA: reference an entity by its id instead of by value

537 views Asked by At

Disclaimer #1: I'm fairly new to XSDs, databases, and JPA - so my question may be nonsensical.

I have an interface specified via an XSD - I'll be receiving XML documents and need to persist their contents to a relational database. I have some limited ability to modify the XSD. The interface itself consists of a large number of entities with complex relationships; there are a number of places where it's not desirable to require the full object graph in the XML document.

Essentially, my XSD currently looks like this:

<xsd:schema 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
     xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations"     
     xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"                                                       
     xmlns:ns1="urn:foo"
     targetNamespace="urn:foo" elementFormDefault="qualified" jxb:version="2.0"
     jxb:extensionBindingPrefixes="hj orm">
     <xsd:complexType name="Foo">
        <xsd:sequence>
            <xsd:element name="fooId" type="xsd:string" nillable="false">
                <xsd:annotation>
                    <xsd:appinfo>
                        <hj:id />
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
     </xsd:complexType>
     <xsd:complexType name="Bar">
        <xsd:sequence>
            <xsd:element name="BarId" type="xsd:string" nillable="false">
                <xsd:annotation>
                    <xsd:appinfo>
                        <hj:id />
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
            <xsd:element name="associatedFoo" type="xsd:string" nillable="false">
            </xsd:element>
        </xsd:sequence>
     </xsd:complexType>
     <xsd:complexType name="Baz">
        <xsd:sequence>
            <xsd:element name="BazId" type="xsd:string" nillable="false">
                <xsd:annotation>
                    <xsd:appinfo>
                        <hj:id />
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
            <xsd:element name="associatedFoos" type="xsd:string" nillable="false" minOccurs="0" maxOccurs="unbounded">
            </xsd:element>
        </xsd:sequence>
     </xsd:complexType>
</xsd:schema>

I run that XSD through hyperjaxb3 (0.5.6) and get a set of Java classes for Foo, Bar, and Baz. What I'd like is for the resulting DB schema to require that Bar.associatedFoo and Baz.associatedFoos are all actual fooIds. However, I can't just update the schema so that e.g. bar.associatedFoo is of type ns1:Foo. So, the question is: how do I do that?

The reasoning here is that each of the types in the XSD are actually large, complex, interrelated object graphs; I don't want the XML document for Baz to have to contain the full XML representation of each of its Foos.

(Obviously, one option is to manually add the DB constraint by hand after the JPA schema creation - but I'd rather embed this information in the XSD if possible.)

In the simpler case (Bar.associatedFoo), I can use the orm:column annotation to directly modify the column definition:

<hj:basic>
    <orm:column column-definition="VARCHAR(255) REFERENCES myschema.Foo"/>
</hj:basic>

but (a) I'm not sure how to extend this attempt to the Baz.associatedFoos case, and (b) I'd prefer something less fragile if possible (the column-definition string requires tweaking any time the foo id length changes, the schema name changes, or the table name of Foo changes; it depends on JPA deciding to CREATE TABLE Bar before CREATE TABLE foo; and no errors are produced until runtime).

If it matters - I'm using openJPA 2.1.1 as my JPA implementation, and Postgres 9.1 as my DB. In the XSD, I can change the ID definition if needed (make it a complexType, a named alias for an xsd:string, or possibly some other changes), and add any xsd:annotations I like, but I can't make many other structural changes to my types.

Thanks in advance!

0

There are 0 answers