Castor Doclet - One to One Relationship
 
 One to One Relationship
 

Castor Doclet can handle one to one relationships automatically. You just need to have a java relation between two classes.

 Master depend example
 

A Master class is linked to a Depend class :

Source code of the Master class :

 
package test;

import java.util.*;

/**
 * @table MASTER
 * @key-generator MAX
 */
public class Master {
    public int getOID(){ return OID; }
    public void setOID(int OID){ this.OID = OID; }

    public Depend getDepend(){ return depend; }

    public void setDepend(Depend depend){ this.depend = depend; }

    /**
     * @primary-key
     */
    private int OID;
    private Depend depend;
}

Source code of the Depend class :

 
package test;

import java.util.*;

/**
 * @table DEPEND
 * @depends Master
 * @key-generator MAX
 */
public class Depend {
    public int getOID(){ return OID; }
    public void setOID(int OID){ this.OID = OID; }

    /**
     * @primary-key
     */
    private int OID;
}

The Depend class is marked a dependent of the Master class. @depends tag value can be the simple class name if both classes are in the same package. If not, the qualified type name (i.e. with package name) must be used.

Generated mapping :

 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapping 
	PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" 
	"http://castor.exolab.org/mapping.dtd">
<mapping>
  <class name="test.Master" key-generator="MAX" identity="OID">
    <map-to table="MASTER" />
    <field name="OID" type="integer">
      <sql name="OID" />
      <bind-xml name="OID" />
    </field>
    <field name="depend" type="test.Depend">
      <sql name="DEPEND_OID" />
      <bind-xml name="depend" />
    </field>
  </class>
  <class name="test.Depend" depends="test.Master" 
  key-generator="MAX" identity="OID">
    <map-to table="DEPEND" />
    <field name="OID" type="integer">
      <sql name="OID" />
      <bind-xml name="OID" />
    </field>
  </class>
</mapping>

and DDL :

 
DROP TABLE DEPEND
;

DROP TABLE MASTER
;

CREATE TABLE DEPEND(
OID INT NOT NULL
, CONSTRAINT PK_DEPEND PRIMARY KEY (OID)
)
;


CREATE TABLE MASTER(
OID INT NOT NULL,
DEPEND_OID INT 
, CONSTRAINT PK_MASTER PRIMARY KEY (OID)
)
;

ALTER TABLE MASTER ADD CONSTRAINT FK_MASTER_DEPEND_O 
	FOREIGN KEY (DEPEND_OID) REFERENCES DEPEND(OID)
;

The Depend class primary key is used as a foreign key column in the Master table. The field type in the mapping file is determined by the java property type. The name of the foreign key column is the name of the foreign table, followed by the primary key column name.

Note that a foreign key constraint is also generated. Its name is limited to 18 characters due to some RDBMS limits.