public class SFieldReference<RI extends SRecordInstance> extends SFieldMeta
The following example shows two foreign keys, one from Employee to Department, and a recursive one from an Employee to their Manager.
static final SFieldReference<Department> DEPARTMENT = new SFieldReference(EMPLOYEE, Department.DEPARTMENT, "DEPT");
public static final SFieldString MANAGER_EMPEE_ID = new SFieldString(EMPLOYEE, "MANAGER_EMPEE_ID", 20);
static final SFieldReference<mployee> 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:-
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.)
quoteName
Constructor and Description |
---|
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.
|
SFieldReference(SRecordMeta<?> meta,
SRecordMeta<RI> referencedRecord,
java.lang.String fieldName,
SFieldScalar... foreignKeys)
Short cut for tables with a single foreign key column.
|
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.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
convertToDataSetFieldType(java.lang.Object raw)
Converts the parameter from the raw type parameter to the correct internal Object
type that is stored in the data set.
|
java.lang.String |
createColumnSQL() |
java.util.Set<SFieldScalar> |
getForeignKeyMetas() |
SFieldScalar |
getPrimaryKeyForForegnKey(SFieldScalar fkey)
The Primary key in the other table refrenced by this foreign key for this reference (might be overlapping).
|
SRecordMeta<RI> |
getReferencedRecordMeta()
The RecordMeta that this reference returns SRecordInstances of.
|
java.lang.Object |
queryFieldValue(java.sql.ResultSet rs,
int sqlIndex)
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.
|
java.lang.String |
toLongerString()
Lists all the details of the field.
|
java.lang.String |
toString()
Lists the record and column name only.
|
addFlag, addValidator, doValidate, equals, getFieldName, getFlags, getRecordMeta, getUserProperties, getUserProperty, getValidators, hashCode, isDescriptive, isForeignKey, isMandatory, isUnqueried, nextGeneratedValue, putUserProperty, readResolve, setQuoted, writeFieldValue, writeFieldValue
public SFieldReference(SRecordMeta<?> meta, SRecordMeta<RI> referencedRecord, java.lang.String fieldName, SFieldScalar[] foreignKeys, SFieldScalar[] referencedPrimaryKeys)
public SFieldReference(SRecordMeta<?> meta, SRecordMeta<RI> referencedRecord, java.lang.String fieldName, SFieldScalar... foreignKeys)
public SFieldReference(SRecordMeta<?> meta, SRecordMeta<RI> referencedRecord, java.lang.String fieldName)
public java.lang.String createColumnSQL()
public java.lang.String toString()
SFieldMeta
toString
in class SFieldMeta
public java.lang.String toLongerString()
SFieldMeta
toLongerString
in class SFieldMeta
public java.util.Set<SFieldScalar> getForeignKeyMetas()
public SFieldScalar getPrimaryKeyForForegnKey(SFieldScalar fkey)
public java.lang.Object queryFieldValue(java.sql.ResultSet rs, int sqlIndex)
SFieldMeta
queryFieldValue
in class SFieldMeta
protected java.lang.Object convertToDataSetFieldType(java.lang.Object raw) throws java.lang.Exception
SFieldMeta
SRecordInstance.setObject
etc., Not getObject.convertToDataSetFieldType
in class SFieldMeta
java.lang.Exception
public SRecordMeta<RI> getReferencedRecordMeta()