Class SFieldReference<RI extends SRecordInstance>
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ReferenceField
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.)
- See Also:
-
Field Summary
Fields inherited from class simpleorm.dataset.SFieldMeta
quoteName
-
Constructor Summary
ConstructorsConstructorDescriptionSFieldReference
(SRecordMeta<?> meta, SRecordMeta<RI> referencedRecord, 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, String fieldName, SFieldScalar... foreignKeys) Short cut for tables with a single foreign key column.SFieldReference
(SRecordMeta<?> meta, SRecordMeta<RI> referencedRecord, String fieldName, SFieldScalar[] foreignKeys, SFieldScalar[] referencedPrimaryKeys) Creates a foreign key field named fieldName that uses foreignKeys to reference referencedRecord.referencedKeys. -
Method Summary
Modifier and TypeMethodDescriptionprotected Object
Converts the parameter from the raw type parameter to the correct internal Object type that is stored in the data set.The Primary key in the other table refrenced by this foreign key for this reference (might be overlapping).The RecordMeta that this reference returns SRecordInstances of.queryFieldValue
(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.Lists all the details of the field.toString()
Lists the record and column name only.Methods inherited from class simpleorm.dataset.SFieldMeta
addFlag, addValidator, doValidate, equals, getFieldName, getFlags, getRecordMeta, getUserProperties, getUserProperty, getValidators, hashCode, isDescriptive, isForeignKey, isMandatory, isUnqueried, nextGeneratedValue, putUserProperty, readResolve, setQuoted, writeFieldValue, writeFieldValue
-
Constructor Details
-
SFieldReference
public SFieldReference(SRecordMeta<?> meta, SRecordMeta<RI> referencedRecord, 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, String fieldName, SFieldScalar... foreignKeys) Short cut for tables with a single foreign key column. -
SFieldReference
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 Details
-
createColumnSQL
-
toString
Description copied from class:SFieldMeta
Lists the record and column name only. Useful in traces.- Overrides:
toString
in classSFieldMeta
-
toLongerString
Description copied from class:SFieldMeta
Lists all the details of the field.- Specified by:
toLongerString
in classSFieldMeta
-
getForeignKeyMetas
-
getPrimaryKeyForForegnKey
The Primary key in the other table refrenced by this foreign key for this reference (might be overlapping). -
queryFieldValue
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 classSFieldMeta
-
convertToDataSetFieldType
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 bySRecordInstance.setObject
etc., Not getObject.- Specified by:
convertToDataSetFieldType
in classSFieldMeta
- Throws:
Exception
-
getReferencedRecordMeta
The RecordMeta that this reference returns SRecordInstances of. (Replaces badly named getReferencedRecord)
-