/** * INTERNAL: * Return the base expression to use for adding fields to the query. * Normally this is the query's builder, but may be the join table for m-m. */ @Override protected Expression getAdditionalFieldsBaseExpression(ReadQuery query) { return ((ReadAllQuery)query).getExpressionBuilder().getTable(getRelationTable()); }
/** * INTERNAL: * Return the base expression to use for adding fields to the query. * Normally this is the query's builder, but may be the join table for m-m. */ @Override protected Expression getAdditionalFieldsBaseExpression(ReadQuery query) { return ((ReadAllQuery)query).getExpressionBuilder().getTable(getRelationTable()); }
/** * INTERNAL: * Verifies listOrderField's table: it must be relation table. * Precondition: listOrderField != null. */ @Override protected void buildListOrderField() { if(this.listOrderField.hasTableName()) { if(!getRelationTable().equals(this.listOrderField.getTable())) { throw DescriptorException.listOrderFieldTableIsWrong(this.getDescriptor(), this, this.listOrderField.getTable(), getRelationTable()); } } else { listOrderField.setTable(getRelationTable()); } this.listOrderField = getDescriptor().buildField(this.listOrderField, getRelationTable()); }
/** * INTERNAL: * Verifies listOrderField's table: it must be relation table. * Precondition: listOrderField != null. */ protected void buildListOrderField() { if(this.listOrderField.hasTableName()) { if(!getRelationTable().equals(this.listOrderField.getTable())) { throw DescriptorException.listOrderFieldTableIsWrong(this.getDescriptor(), this, this.listOrderField.getTable(), getRelationTable()); } } else { listOrderField.setTable(getRelationTable()); } this.listOrderField = getDescriptor().buildField(this.listOrderField, getRelationTable()); }
/** * INTERNAL: * Verifies listOrderField's table: it must be relation table. * Precondition: listOrderField != null. */ @Override protected void buildListOrderField() { if(this.listOrderField.hasTableName()) { if(!getRelationTable().equals(this.listOrderField.getTable())) { throw DescriptorException.listOrderFieldTableIsWrong(this.getDescriptor(), this, this.listOrderField.getTable(), getRelationTable()); } } else { listOrderField.setTable(getRelationTable()); } this.listOrderField = getDescriptor().buildField(this.listOrderField, getRelationTable()); }
protected void addManyToManyMappingLines(NonreflectiveMethodDefinition method, String mappingName, ManyToManyMapping mapping) { if (mapping.getRelationTable() != null) { method.addLine(mappingName + ".setRelationTableName(\"" + mapping.getRelationTable().getQualifiedName() + "\");"); } Enumeration sourceRelationKeysEnum = mapping.getSourceRelationKeyFields().elements(); Enumeration sourceKeysEnum = mapping.getSourceKeyFields().elements(); while (sourceRelationKeysEnum.hasMoreElements()) { DatabaseField sourceField = (DatabaseField)sourceKeysEnum.nextElement(); DatabaseField relationField = (DatabaseField)sourceRelationKeysEnum.nextElement(); method.addLine(mappingName + ".addSourceRelationKeyFieldName(\"" + relationField.getQualifiedName() + "\", \"" + sourceField.getQualifiedName() + "\");"); } Enumeration targetRelationKeysEnum = mapping.getTargetRelationKeyFields().elements(); Enumeration targetKeysEnum = mapping.getTargetKeyFields().elements(); while (targetRelationKeysEnum.hasMoreElements()) { DatabaseField targetField = (DatabaseField)targetKeysEnum.nextElement(); DatabaseField relationField = (DatabaseField)targetRelationKeysEnum.nextElement(); method.addLine(mappingName + ".addTargetRelationKeyFieldName(\"" + relationField.getQualifiedName() + "\", \"" + targetField.getQualifiedName() + "\");"); } addHistoryPolicyLines(method, mapping, mappingName); }
protected void addManyToManyMappingLines(NonreflectiveMethodDefinition method, String mappingName, ManyToManyMapping mapping) { if (mapping.getRelationTable() != null) { method.addLine(mappingName + ".setRelationTableName(\"" + mapping.getRelationTable().getQualifiedName() + "\");"); } Enumeration sourceRelationKeysEnum = mapping.getSourceRelationKeyFields().elements(); Enumeration sourceKeysEnum = mapping.getSourceKeyFields().elements(); while (sourceRelationKeysEnum.hasMoreElements()) { DatabaseField sourceField = (DatabaseField)sourceKeysEnum.nextElement(); DatabaseField relationField = (DatabaseField)sourceRelationKeysEnum.nextElement(); method.addLine(mappingName + ".addSourceRelationKeyFieldName(\"" + relationField.getQualifiedName() + "\", \"" + sourceField.getQualifiedName() + "\");"); } Enumeration targetRelationKeysEnum = mapping.getTargetRelationKeyFields().elements(); Enumeration targetKeysEnum = mapping.getTargetKeyFields().elements(); while (targetRelationKeysEnum.hasMoreElements()) { DatabaseField targetField = (DatabaseField)targetKeysEnum.nextElement(); DatabaseField relationField = (DatabaseField)targetRelationKeysEnum.nextElement(); method.addLine(mappingName + ".addTargetRelationKeyFieldName(\"" + relationField.getQualifiedName() + "\", \"" + targetField.getQualifiedName() + "\");"); } addHistoryPolicyLines(method, mapping, mappingName); }
protected void addManyToManyMappingLines(NonreflectiveMethodDefinition method, String mappingName, ManyToManyMapping mapping) { if (mapping.getRelationTable() != null) { method.addLine(mappingName + ".setRelationTableName(\"" + mapping.getRelationTable().getQualifiedName() + "\");"); } Enumeration sourceRelationKeysEnum = mapping.getSourceRelationKeyFields().elements(); Enumeration sourceKeysEnum = mapping.getSourceKeyFields().elements(); while (sourceRelationKeysEnum.hasMoreElements()) { DatabaseField sourceField = (DatabaseField)sourceKeysEnum.nextElement(); DatabaseField relationField = (DatabaseField)sourceRelationKeysEnum.nextElement(); method.addLine(mappingName + ".addSourceRelationKeyFieldName(\"" + relationField.getQualifiedName() + "\", \"" + sourceField.getQualifiedName() + "\");"); } Enumeration targetRelationKeysEnum = mapping.getTargetRelationKeyFields().elements(); Enumeration targetKeysEnum = mapping.getTargetKeyFields().elements(); while (targetRelationKeysEnum.hasMoreElements()) { DatabaseField targetField = (DatabaseField)targetKeysEnum.nextElement(); DatabaseField relationField = (DatabaseField)targetRelationKeysEnum.nextElement(); method.addLine(mappingName + ".addTargetRelationKeyFieldName(\"" + relationField.getQualifiedName() + "\", \"" + targetField.getQualifiedName() + "\");"); } addHistoryPolicyLines(method, mapping, mappingName); }
/** * INTERNAL: * Return the reference table for this accessor. If it is a many to many * mapping, return the join table otherwise return the reference descriptors * primary key table. */ @Override protected DatabaseTable getReferenceDatabaseTable() { if (getMapping().isManyToManyMapping()) { return ((ManyToManyMapping) getMapping()).getRelationTable(); } else { return super.getReferenceDatabaseTable(); } }
@Override public void customize(final Session session) throws Exception { if (JPAThreadContext.infos.containsKey("properties")) { final String prefix = ((Properties) JPAThreadContext.infos.get("properties")).getProperty("openejb.jpa.table_prefix"); final List<DatabaseTable> tables = new ArrayList<DatabaseTable>(); for (final ClassDescriptor cd : session.getDescriptors().values()) { for (final DatabaseTable table : cd.getTables()) { update(prefix, tables, table); } for (final DatabaseMapping mapping : cd.getMappings()) { if (mapping instanceof ManyToManyMapping) { update(prefix, tables, ((ManyToManyMapping) mapping).getRelationTable()); } else if (mapping instanceof DirectCollectionMapping) { update(prefix, tables, ((DirectCollectionMapping) mapping).getReferenceTable()); } // TODO: else check we need to update something } } final Sequence sequence = session.getDatasourcePlatform().getDefaultSequence(); if (sequence instanceof TableSequence) { final TableSequence ts = ((TableSequence) sequence); ts.setName(prefix + ts.getName()); } } }
((ManyToManyMapping) mapping).setRelationTable(updateTable(((ManyToManyMapping) mapping).getRelationTable()));
/** * INTERNAL: * Add additional fields and check for history. */ protected void postPrepareNestedBatchQuery(ReadQuery batchQuery, ReadAllQuery query) { ReadAllQuery mappingBatchQuery = (ReadAllQuery)batchQuery; mappingBatchQuery.setShouldIncludeData(true); for (Enumeration relationFieldsEnum = getSourceRelationKeyFields().elements(); relationFieldsEnum.hasMoreElements();) { mappingBatchQuery.getAdditionalFields().add(mappingBatchQuery.getExpressionBuilder().getTable(getRelationTable()).getField((DatabaseField)relationFieldsEnum.nextElement())); } if (getHistoryPolicy() != null) { ExpressionBuilder builder = mappingBatchQuery.getExpressionBuilder(); Expression twisted = batchQuery.getSelectionCriteria(); if (query.getSession().getAsOfClause() != null) { builder.asOf(query.getSession().getAsOfClause()); } else if (builder.getAsOfClause() == null) { builder.asOf(AsOfClause.NO_CLAUSE); } twisted = twisted.and(getHistoryPolicy().additionalHistoryExpression(builder)); mappingBatchQuery.setSelectionCriteria(twisted); } }
/** * 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 = getSourceRelationKeyFields().elementAt(index); DatabaseField sourceKey = getSourceKeyFields().elementAt(index); subExpression = builder.getField(sourceRelationKey).equal(builder.getParameter(sourceKey)); expression = subExpression.and(expression); } // All the entries are deleted in one shot. statement.setWhereClause(expression); statement.setTable(getRelationTable()); getDeleteAllQuery().setSQLStatement(statement); }
/** * INTERNAL: * This method is used to update the table per tenant descriptor with * a table per tenant schema. This includes any relation tables from * mappings. This will be done through the setting of a table qualifier on * the tables. * * This method should only be called at the start of a client session * lifecycle and should only be called once. */ protected void setTableSchemaPerTenant() { descriptor.setTableQualifier(contextTenant); // Any mapping with a relation table will need to be updated. for (DatabaseMapping mapping : descriptor.getMappings()) { if (mapping.isManyToManyMapping()) { ((ManyToManyMapping) mapping).getRelationTable().setTableQualifier(contextTenant); } else if (mapping.isOneToOneMapping() && ((OneToOneMapping) mapping).hasRelationTable()) { ((OneToOneMapping) mapping).getRelationTable().setTableQualifier(contextTenant); } else if (mapping.isDirectCollectionMapping()) { ((DirectCollectionMapping) mapping).getReferenceTable().setTableQualifier(contextTenant); } } }
/** * INTERNAL: * This method is used to update the table per tenant descriptor with * a table per tenant schema. This includes any relation tables from * mappings. This will be done through the setting of a table qualifier on * the tables. * * This method should only be called at the start of a client session * lifecycle and should only be called once. */ protected void setTableSchemaPerTenant() { descriptor.setTableQualifier(contextTenant); // Any mapping with a relation table will need to be updated. for (DatabaseMapping mapping : descriptor.getMappings()) { if (mapping.isManyToManyMapping()) { ((ManyToManyMapping) mapping).getRelationTable().setTableQualifier(contextTenant); } else if (mapping.isOneToOneMapping() && ((OneToOneMapping) mapping).hasRelationTable()) { ((OneToOneMapping) mapping).getRelationTable().setTableQualifier(contextTenant); } else if (mapping.isDirectCollectionMapping()) { ((DirectCollectionMapping) mapping).getReferenceTable().setTableQualifier(contextTenant); } } }
statement.setTable(getRelationTable()); getDeleteAllQuery().setSQLStatement(statement);
/** * Calculate the relation table for based on the various QueryKeyExpression * usages (join query keys, custom defined query keys, or query keys for * mappings). * * Called from {@link SQLSelectStatement#appendFromClauseForOuterJoin}. * * @return DatabaseTable */ public DatabaseTable getRelationTable() { if(getMapping() != null) { if(getMapping().isManyToManyMapping()) { return ((ManyToManyMapping)getMapping()).getRelationTable(); } else if(getMapping().isOneToOneMapping()) { return ((OneToOneMapping)getMapping()).getRelationTable(); } } else { if(getQueryKeyOrNull().isForeignReferenceQueryKey()) { return ((ForeignReferenceQueryKey)getQueryKeyOrNull()).getRelationTable(getDescriptor()); } } return null; } }
/** * Calculate the relation table for based on the various QueryKeyExpression * usages (join query keys, custom defined query keys, or query keys for * mappings). * * Called from {@link SQLSelectStatement#appendFromClauseForOuterJoin}. * * @return DatabaseTable */ @Override public DatabaseTable getRelationTable() { if(getMapping() != null) { if(getMapping().isManyToManyMapping()) { return ((ManyToManyMapping)getMapping()).getRelationTable(); } else if(getMapping().isOneToOneMapping()) { return ((OneToOneMapping)getMapping()).getRelationTable(); } } else { if(getQueryKeyOrNull().isForeignReferenceQueryKey()) { return ((ForeignReferenceQueryKey)getQueryKeyOrNull()).getRelationTable(getDescriptor()); } } return null; } }
statement.setTable(getRelationTable()); getDeleteAllQuery().setSQLStatement(statement);
/** * Calculate the relation table for based on the various QueryKeyExpression * usages (join query keys, custom defined query keys, or query keys for * mappings). * * Called from {@link SQLSelectStatement#appendFromClauseForOuterJoin}. * * @return DatabaseTable */ public DatabaseTable getRelationTable() { if(getMapping() != null) { if(getMapping().isManyToManyMapping()) { return ((ManyToManyMapping)getMapping()).getRelationTable(); } else if(getMapping().isOneToOneMapping()) { return ((OneToOneMapping)getMapping()).getRelationTable(); } } else { if(getQueryKeyOrNull().isForeignReferenceQueryKey()) { return ((ForeignReferenceQueryKey)getQueryKeyOrNull()).getRelationTable(getDescriptor()); } } return null; } }