Mapping SDO Types to Java source code

Generate Plain, Old, Java Objects from SDO

SDO Types specified in XML Schema Definition (*.xsd) files can be converted to Java classes annotated for persistence.

 $ java org.apache.openjpa.sdo.SDO2POJOGenerator person.xsd

will convert one or more SDO types specified in person.xsd, annotate them for persitence and save them in corresponding *.java file. The generated Java source files are written relative to the current directory.

Specify -rootDirectory or simply -d option to write to another location in the disk. For example,

 $ java org.apache.openjpa.sdo.SDO2POJOGenerator -d generated/src/ p.xsd q.xsd

will convert SDO Types from both p.xsd and q.xsd to Java source code and write them to a directory relative to generated/src/.

Rules of SDO to POJO mapping

The SDO Type are mapped isomorphically to Java classes (with one exception).

  • Each property of primitive xsd type is converted to Java field of same name and decalred type is as specified by SDO specification for primitive type conversion. LINK
  • Each single-valued property of user defined type X is converted to a relation field of Java equivalent for type X.
  • Each multi-valued property of user defined type X is converted to a relation field of java.util.List whose element is Java equivalent of type X.
  • One exception is made for Container type. Container type is defined as a SDO Type that has one and only one many-valued Property of user-defined type. This single property is referred as component type.
  • All Java fields will be private with bean-style getter and setter methods.

    No Java source code is generated for Container type.

    The property of Container type is mapped as java.util.List whose element is Java equivalent of component type.

Added JPA Annotation

The generated Java source code is annotated with JPA annotations.

  • The generated class is marked as @Entity
  • Identity field is annotated with @Id and @GeneratedValue
  • Version field is annotated with @Version
  • Single-valued field is annoted with @OneToOne
  • Many-valued field and container field is annotated with @OneToMany

Using Package name for generated Java source

Java package name for SDO Types can be specified in sdoJava:package in *.xsd header. For example,

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:example="http://www.example.com/Example" 
    targetNamespace="http://www.example.com/Example"
    
    xmlns:sdo="commonj.sdo"
    xmlns:sdoJava="commonj.sdo/java"
    sdoJava:package="example.domain">

    <xsd:complexType name="Person">
       ....

will generate a Java class with fully-qualified name example.domain.Person.