/** * INTERNAL: * Set the table qualifier on the relation table if required */ protected void initializeRelationTable(AbstractSession session) throws DescriptorException { Platform platform = session.getDatasourcePlatform(); if ((getRelationTable() == null) || (getRelationTableName().length() == 0)) { throw DescriptorException.noRelationTable(this); } if (platform.getTableQualifier().length() == 0) { return; } if (getRelationTable().getTableQualifier().length() == 0) { getRelationTable().setTableQualifier(platform.getTableQualifier()); } }
/** * INTERNAL: * All the source relation key field names are converted to DatabaseField and stored. */ protected void initializeSourceRelationKeys(AbstractSession session) throws DescriptorException { if (getSourceRelationKeyFields().size() == 0) { throw DescriptorException.noSourceRelationKeysSpecified(this); } for (Enumeration entry = getSourceRelationKeyFields().elements(); entry.hasMoreElements();) { DatabaseField field = (DatabaseField)entry.nextElement(); if (field.hasTableName() && (!(field.getTableName().equals(getRelationTable().getName())))) { throw DescriptorException.relationKeyFieldNotProperlySpecified(field, this); } field.setTable(getRelationTable()); } }
/** * INTERNAL: * All the target relation key field names are converted to DatabaseField and stored. */ protected void initializeTargetRelationKeys(AbstractSession session) { if (getTargetRelationKeyFields().size() == 0) { throw DescriptorException.noTargetRelationKeysSpecified(this); } for (Enumeration targetEnum = getTargetRelationKeyFields().elements(); targetEnum.hasMoreElements();) { DatabaseField field = (DatabaseField)targetEnum.nextElement(); if (field.hasTableName() && (!(field.getTableName().equals(getRelationTable().getName())))) { throw DescriptorException.relationKeyFieldNotProperlySpecified(field, this); } field.setTable(getRelationTable()); } }
DatabaseTable newTarget = ((ManyToManyMapping)outerExpression.getMapping()).getRelationTable(); DatabaseTable newAlias = relationExpression.aliasForTable(newTarget); writer.write(", OUTER ");// need to outer join only to relation table for many-to-many case in Informix
/** * Build relation table definitions for all many-to-many relationships in a TopLink desciptor. */ private void buildRelationTableDefinition(ManyToManyMapping mapping) { //first create relation table TableDefinition tblDef = getTableDefFromDBTable(mapping.getRelationTable()); //add source foreign key fields into the relation table Vector srcFkFields = mapping.getSourceRelationKeyFields(); Vector srcKeyFields = mapping.getSourceKeyFields(); buildRelationTableFields(tblDef, srcFkFields, srcKeyFields); //add target foreign key fields into the relation table Vector targFkFields = mapping.getTargetRelationKeyFields(); Vector targKeyFields = mapping.getTargetKeyFields(); buildRelationTableFields(tblDef, targFkFields, targKeyFields); }
/** * INTERNAL: * Initialize insert query. This query is used to insert the collection of objects into the * relation table. */ protected void initializeInsertQuery(AbstractSession session) { if (!getInsertQuery().hasSessionName()) { getInsertQuery().setSessionName(session.getName()); } if (hasCustomInsertQuery()) { return; } SQLInsertStatement statement = new SQLInsertStatement(); statement.setTable(getRelationTable()); AbstractRecord joinRow = new DatabaseRecord(); for (Enumeration targetEnum = getTargetRelationKeyFields().elements(); targetEnum.hasMoreElements();) { joinRow.put((DatabaseField)targetEnum.nextElement(), null); } for (Enumeration sourceEnum = getSourceRelationKeyFields().elements(); sourceEnum.hasMoreElements();) { joinRow.put((DatabaseField)sourceEnum.nextElement(), null); } statement.setModifyRow(joinRow); getInsertQuery().setSQLStatement(statement); getInsertQuery().setModifyRow(joinRow); }
DatabaseTable relationTable = ((ManyToManyMapping)outerExpression.getMapping()).getRelationTable(); DatabaseTable relationAlias = ((Expression)getOuterJoinedMappingCriteria().elementAt(index)).aliasForTable(relationTable); writer.write(" LEFT OUTER JOIN (");
statement.setTable(getRelationTable()); statement.setWhereClause(whereClause); getDeleteQuery().setSQLStatement(statement);
/** * Initialize delete all query. This query is used to all relevant rows from the * relation table. */ protected void initializeDeleteAllQuery(AbstractSession session) { if (!getDeleteAllQuery().hasSessionName()) { getDeleteAllQuery().setSessionName(session.getName()); } if (hasCustomDeleteAllQuery()) { return; } Expression expression = null; Expression subExpression; Expression builder = new ExpressionBuilder(); SQLDeleteStatement statement = new SQLDeleteStatement(); // Construct an expression to delete from the relation table. for (int index = 0; index < getSourceRelationKeyFields().size(); index++) { DatabaseField sourceRelationKey = (DatabaseField)getSourceRelationKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); subExpression = builder.getField(sourceRelationKey).equal(builder.getParameter(sourceKey)); expression = subExpression.and(expression); } // All the enteries are deleted in one shot. statement.setWhereClause(expression); statement.setTable(getRelationTable()); getDeleteAllQuery().setSQLStatement(statement); }