/** * INTERNAL: * Return the reference key field names associated with the mapping. * These are in-order with the sourceKeyFieldNames. */ public Vector getReferenceKeyFieldNames() { Vector fieldNames = new Vector(getReferenceKeyFields().size()); for (Enumeration fieldsEnum = getReferenceKeyFields().elements(); fieldsEnum.hasMoreElements();) { fieldNames.addElement(((DatabaseField)fieldsEnum.nextElement()).getQualifiedName()); } return fieldNames; }
/** * PUBLIC: * Set the name of the reference key field. * This is the foreign key field in the direct table referencing the primary key of the source object. * This method is used if the reference key consists of only a single field. */ public void setReferenceKeyFieldName(String fieldName) { getReferenceKeyFields().addElement(new DatabaseField(fieldName)); }
/** * PUBLIC: * Add the reference key field. * This is used for composite reference keys. * This is the foreign key field in the direct table referencing the primary key of the source object. * Both the reference field and the source field that it references must be provided. */ public void addReferenceKeyField(DatabaseField referenceForeignKeyField, DatabaseField sourcePrimaryKeyField) { getSourceKeyFields().addElement(sourcePrimaryKeyField); getReferenceKeyFields().addElement(referenceForeignKeyField); }
/** * The reference keys on the reference table are initalized */ protected void initializeReferenceKeys(AbstractSession session) throws DescriptorException { if (getReferenceKeyFields().size() == 0) { throw DescriptorException.noReferenceKeyIsSpecified(this); } for (Enumeration referenceEnum = getReferenceKeyFields().elements(); referenceEnum.hasMoreElements();) { DatabaseField field = (DatabaseField)referenceEnum.nextElement(); if (field.hasTableName() && (!(field.getTableName().equals(getReferenceTable().getName())))) { throw DescriptorException.referenceKeyFieldNotProperlySpecified(field, this); } field.setTable(getReferenceTable()); } }
/** * INTERNAL: * Extract the source primary key value from the reference direct row. * Used for batch reading, most following same order and fields as in the mapping. */ protected Vector extractKeyFromReferenceRow(AbstractRecord row, AbstractSession session) { Vector key = new Vector(getReferenceKeyFields().size()); for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField relationField = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceField = (DatabaseField)getSourceKeyFields().elementAt(index); Object value = row.get(relationField); // Must ensure the classificatin to get a cache hit. try { value = session.getDatasourcePlatform().getConversionManager().convertObject(value, getDescriptor().getObjectBuilder().getFieldClassification(sourceField)); } catch (ConversionException e) { throw ConversionException.couldNotBeConverted(this, getDescriptor(), e); } key.addElement(value); } return key; }
protected void initializeSelectionCriteria(AbstractSession session) { Expression exp1; Expression exp2; Expression expression; Expression criteria = null; Enumeration referenceKeysEnum; Enumeration sourceKeysEnum; ExpressionBuilder base = new ExpressionBuilder(); TableExpression table = (TableExpression)base.getTable(getReferenceTable()); referenceKeysEnum = getReferenceKeyFields().elements(); sourceKeysEnum = getSourceKeyFields().elements(); for (; referenceKeysEnum.hasMoreElements();) { DatabaseField referenceKey = (DatabaseField)referenceKeysEnum.nextElement(); DatabaseField sourceKey = (DatabaseField)sourceKeysEnum.nextElement(); exp1 = table.getField(referenceKey); exp2 = base.getParameter(sourceKey); expression = exp1.equal(exp2); if (criteria == null) { criteria = expression; } else { criteria = expression.and(criteria); } } setSelectionCriteria(criteria); }
} else if(mapping.isDirectCollectionMapping()) { sourceFields = ((DirectCollectionMapping)mapping).getSourceKeyFields(); targetFields = ((DirectCollectionMapping)mapping).getReferenceKeyFields();
/** * INTERNAL: * The mapping clones itself to create deep copy. */ public Object clone() { DirectCollectionMapping clone = (DirectCollectionMapping)super.clone(); clone.setSourceKeyFields(cloneFields(getSourceKeyFields())); clone.setReferenceKeyFields(cloneFields(getReferenceKeyFields())); return clone; }
} else if(mapping.isDirectCollectionMapping()) { sourceFields = ((DirectCollectionMapping)mapping).getSourceKeyFields(); targetFields = ((DirectCollectionMapping)mapping).getReferenceKeyFields();
/** * Build direct collection table definitions in a TopLink desciptor */ private void buildDirectCollectionTableDefinition(DirectCollectionMapping mapping, ClassDescriptor desc) { //first create direct collection table TableDefinition tblDef = getTableDefFromDBTable(mapping.getReferenceTable()); DatabaseField dbField = null; //add the table reference key(s) Vector refPkFields = mapping.getReferenceKeyFields(); for (int index = 0; index < refPkFields.size(); index++) { dbField = resolveDatabaseField((DatabaseField) refPkFields.get(index), (DatabaseField) mapping.getSourceKeyFields().get(index)); tblDef.addField(getDirectCollectionReferenceKeyFieldDefFromDBField(dbField)); } //add the direct collection field to the table. tblDef.addField(getFieldDefFromDBField(mapping.getDirectField(), false)); //if the mapping is direct-map field, add the direct key field to the table as well. if (mapping.isDirectMapMapping()) { dbField = ((DirectMapMapping) mapping).getDirectKeyField(); tblDef.addField(getFieldDefFromDBField(dbField, false)); } }
for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index);
protected void initializeDeleteQuery(AbstractSession session) { if (!getDeleteQuery().hasSessionName()) { getDeleteQuery().setSessionName(session.getName()); } if (hasCustomDeleteQuery()) { return; } Expression builder = new ExpressionBuilder(); Expression directExp = builder.getField(getDirectField()).equal(builder.getParameter(getDirectField())); Expression expression = null; SQLDeleteStatement statement = new SQLDeleteStatement(); // Construct an expression to delete from the relation table. for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); Expression subExp1 = builder.getField(referenceKey); Expression subExp2 = builder.getParameter(sourceKey); Expression subExpression = subExp1.equal(subExp2); expression = subExpression.and(expression); } expression = expression.and(directExp); statement.setWhereClause(expression); statement.setTable(getReferenceTable()); getDeleteQuery().setSQLStatement(statement); }
/** * Initialize insert query. This query is used to insert the collection of objects into the * reference table. */ protected void initializeInsertQuery(AbstractSession session) { if (!getInsertQuery().hasSessionName()) { getInsertQuery().setSessionName(session.getName()); } if (hasCustomInsertQuery()) { return; } SQLInsertStatement statement = new SQLInsertStatement(); statement.setTable(getReferenceTable()); AbstractRecord directRow = new DatabaseRecord(); for (Enumeration referenceEnum = getReferenceKeyFields().elements(); referenceEnum.hasMoreElements();) { directRow.put((DatabaseField)referenceEnum.nextElement(), null); } directRow.put(getDirectField(), null); statement.setModifyRow(directRow); getInsertQuery().setSQLStatement(statement); getInsertQuery().setModifyRow(directRow); }
return; for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); Object sourceKeyValue = writeQuery.getTranslationRow().get(sourceKey);
for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); Object sourceKeyValue = query.getTranslationRow().get(sourceKey);