Class SFieldReference<RI extends SRecordInstance>

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    ReferenceField

    public class SFieldReference<RI extends SRecordInstance>
    extends SFieldMeta
    Represents a foreign key reference from one SRecordMeta to another.

    The following example shows two foreign keys, one from Employee to Department, and a recursive one from an Employee to their Manager.

    public static final SFieldString DEPT_ID = new SFieldString(EMPLOYEE, "DEPT_ID", 10); <p> static final SFieldReference&lt;Department&gt; DEPARTMENT = new SFieldReference(EMPLOYEE, Department.DEPARTMENT, "DEPT"); <p> public static final SFieldString MANAGER_EMPEE_ID = new SFieldString(EMPLOYEE, "MANAGER_EMPEE_ID", 20); <p> static final SFieldReference&lt;mployee&gt; MANAGER // Recursive Reference = new SFieldReference(EMPLOYEE, Employee.EMPLOYEE, "MANAGER", MANAGER_EMPEE_ID);

    The DEPT_ID field represents the scalar column in the table that contains the department id string. The DEPARTMENT field above contains a reference to the actual Departement object in memory (which will be retrieved from disk if needed). (This allows DEPT_ID to be seen without retrieving the DEPARTMENT record at all.)

    Between them they create the DDL along the following lines:-

    CONSTRAINT XX_EMPLOYEE_6_DEPT FOREIGN KEY (DEPT_ID) REFERENCES XX_DEPARTMENT (DEPT_ID)

    The Foreign key fields are normally assumed to have the same name as the referenced primary key fields, which is generally a sound practice. But this can be overriden where needed for cases such as the recursive MANAGER reference. Mutli column keys are also supported.

    (It is strongly recommended that tables be given key names such as "EMPLOYEE_ID" rather than just "ID". This enables natrual joins to be performed in SQL, and facilitates many end user reporting tools.)

    "Overlapping" foreign keys are supported. Ie. Given SRecordMeta T(_K_, A, B, C) one can define reference T.R which uses T.A and T.B to reference table U, and also define reference T.S which uses T.A and T.C to reference table V. So SRecordReference R and S overlap by both using scalar field A.

    Any attempt to change the value of a referencing scalar field (such as DEPT_ID above) automatically nulls the corresponding reference field (DEPARTMENT). This includes updates via Overlapping foreign keys.

    (Earlier versions of SimpleOrm (before 3.0) could automatically create the referenced scalar fields given the reference. Ie. it automatically mapped from the conceptual to relational model. This was cool, but it was considered to be too complex and has been removed.)

    See Also:
    Serialized Form
    • Constructor Detail

      • SFieldReference

        public SFieldReference​(SRecordMeta<?> meta,
                               SRecordMeta<RI> referencedRecord,
                               java.lang.String fieldName,
                               SFieldScalar[] foreignKeys,
                               SFieldScalar[] referencedPrimaryKeys)
        Creates a foreign key field named fieldName that uses foreignKeys to reference referencedRecord.referencedKeys. The foreign and referenced scalar fields are explicitly enumerated, so no naming convention is assumed.

      • SFieldReference

        public SFieldReference​(SRecordMeta<?> meta,
                               SRecordMeta<RI> referencedRecord,
                               java.lang.String fieldName,
                               SFieldScalar... foreignKeys)
        Short cut for tables with a single foreign key column.
      • SFieldReference

        public SFieldReference​(SRecordMeta<?> meta,
                               SRecordMeta<RI> referencedRecord,
                               java.lang.String fieldName)
        Shortcut that creates a foreign key field named fieldName that assumes that the names of the scalar foreign keys are the same as the referenced private keys.
    • Method Detail

      • createColumnSQL

        public java.lang.String createColumnSQL()
      • toString

        public java.lang.String toString()
        Description copied from class: SFieldMeta
        Lists the record and column name only. Useful in traces.
        Overrides:
        toString in class SFieldMeta
      • toLongerString

        public java.lang.String toLongerString()
        Description copied from class: SFieldMeta
        Lists all the details of the field.
        Specified by:
        toLongerString in class SFieldMeta
      • getForeignKeyMetas

        public java.util.Set<SFieldScalar> getForeignKeyMetas()
      • getPrimaryKeyForForegnKey

        public SFieldScalar getPrimaryKeyForForegnKey​(SFieldScalar fkey)
        The Primary key in the other table refrenced by this foreign key for this reference (might be overlapping).
      • queryFieldValue

        public java.lang.Object queryFieldValue​(java.sql.ResultSet rs,
                                                int sqlIndex)
        Description copied from class: SFieldMeta
        Issues a JDBC get*() on the result set for the field and converts the database type to the appropriate internal type, eg, Double for a double field. The first column has sqlIndex==1.
        Specified by:
        queryFieldValue in class SFieldMeta
      • convertToDataSetFieldType

        protected java.lang.Object convertToDataSetFieldType​(java.lang.Object raw)
                                                      throws java.lang.Exception
        Description copied from class: SFieldMeta
        Converts the parameter from the raw type parameter to the correct internal Object type that is stored in the data set. Returns the object if no conversion necessary. Used by SRecordInstance.setObject etc., Not getObject.
        Specified by:
        convertToDataSetFieldType in class SFieldMeta
        Throws:
        java.lang.Exception
      • getReferencedRecordMeta

        public SRecordMeta<RI> getReferencedRecordMeta()
        The RecordMeta that this reference returns SRecordInstances of. (Replaces badly named getReferencedRecord)