use of org.datanucleus.metadata.RelationType in project datanucleus-rdbms by datanucleus.
the class SQLStatementHelper method selectMemberOfSourceInStatement.
/**
* Method to select the specified member (field/property) of the source table in the passed SQL
* statement. This populates the mappingDefinition with the column details for this member.
* @param stmt The SQL statement
* @param mappingDefinition Mapping definition for the results (will be populated by any
* selected mappings if provided as input)
* @param fetchPlan FetchPlan
* @param sourceSqlTbl Table that has the member (or a super-table/secondary-table of this table)
* @param mmd Meta-data for the field/property in the source that we are selecting
* @param clr ClassLoader resolver
* @param maxFetchPlanLimit Max fetch depth from this point to select (0 implies no other objects)
* @param inputJoinType Optional join type to use for subobjects (otherwise decide join type internally)
*/
public static void selectMemberOfSourceInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, FetchPlan fetchPlan, SQLTable sourceSqlTbl, AbstractMemberMetaData mmd, ClassLoaderResolver clr, int maxFetchPlanLimit, JoinType inputJoinType) {
boolean selectSubobjects = false;
if (maxFetchPlanLimit > 0) {
selectSubobjects = true;
}
// Set table-group name for any related object we join to (naming based on member name)
String tableGroupName = sourceSqlTbl.getGroupName() + "." + mmd.getName();
JavaTypeMapping m = sourceSqlTbl.getTable().getMemberMapping(mmd);
if (m != null && m.includeInFetchStatement()) {
RelationType relationType = mmd.getRelationType(clr);
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
DatastoreAdapter dba = storeMgr.getDatastoreAdapter();
if (!dba.validToSelectMappingInStatement(stmt, m)) {
// Not valid to select this mapping for this statement so return
return;
}
MetaDataManager mmgr = storeMgr.getMetaDataManager();
StatementMappingIndex stmtMapping = new StatementMappingIndex(m);
if (m.getNumberOfDatastoreMappings() > 0) {
// Select of fields with columns in source table(s)
// Adds inner/outer join to any required superclass/secondary tables
SQLTable sqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(stmt, sourceSqlTbl, m);
boolean selectFK = true;
if (selectSubobjects && (relationType == RelationType.ONE_TO_ONE_UNI || (relationType == RelationType.ONE_TO_ONE_BI && mmd.getMappedBy() == null)) && !mmd.isSerialized() && !mmd.isEmbedded()) {
// Related object with FK at this side
selectFK = selectFetchPlanFieldsOfFKRelatedObject(stmt, mappingDefinition, fetchPlan, sourceSqlTbl, mmd, clr, maxFetchPlanLimit, m, tableGroupName, stmtMapping, sqlTbl, inputJoinType);
} else if (selectSubobjects && (!mmd.isEmbedded() && !mmd.isSerialized()) && relationType == RelationType.MANY_TO_ONE_BI) {
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
if (mmd.getJoinMetaData() != null || relatedMmds[0].getJoinMetaData() != null) {
// N-1 bidirectional join table relation
// TODO Add left outer join from {sourceTable}.ID to {joinTable}.ELEM_FK
Table joinTable = storeMgr.getTable(relatedMmds[0]);
DatastoreElementContainer collTable = (DatastoreElementContainer) joinTable;
JavaTypeMapping selectMapping = collTable.getOwnerMapping();
SQLTable joinSqlTbl = null;
if (stmt.getPrimaryTable().getTable() != joinTable) {
// Join to the join table
JavaTypeMapping referenceMapping = collTable.getElementMapping();
joinSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sourceSqlTbl, sourceSqlTbl.getTable().getIdMapping(), collTable, null, referenceMapping, null, tableGroupName, true);
} else {
// Main table of the statement is the join table so no need to join
joinSqlTbl = stmt.getPrimaryTable();
}
// Select the owner mapping of the join table
int[] colNumbers = stmt.select(joinSqlTbl, selectMapping, null);
stmtMapping.setColumnPositions(colNumbers);
// TODO Join to 1 side from join table?
} else {
// N-1 bidirectional FK relation
// Related object with FK at this side, so join/select related object as required
selectFK = selectFetchPlanFieldsOfFKRelatedObject(stmt, mappingDefinition, fetchPlan, sourceSqlTbl, mmd, clr, maxFetchPlanLimit, m, tableGroupName, stmtMapping, sqlTbl, inputJoinType);
}
}
if (selectFK) {
int[] colNumbers = stmt.select(sqlTbl, m, null);
stmtMapping.setColumnPositions(colNumbers);
}
} else {
// Select of related objects with FK in other table
if (relationType == RelationType.ONE_TO_ONE_BI && mmd.getMappedBy() != null) {
// 1-1 bidirectional relation with FK in related table
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
AbstractMemberMetaData relatedMmd = relatedMmds[0];
String[] clsNames = null;
if (mmd.getType().isInterface()) {
if (mmd.getFieldTypes() != null && mmd.getFieldTypes().length == 1) {
// Use field-type since only one class specified
Class fldTypeCls = clr.classForName(mmd.getFieldTypes()[0]);
if (fldTypeCls.isInterface()) {
// User has specified an interface, so find its implementations
clsNames = mmgr.getClassesImplementingInterface(mmd.getFieldTypes()[0], clr);
} else {
// Use user-provided field-type
clsNames = new String[] { mmd.getFieldTypes()[0] };
}
}
if (clsNames == null) {
clsNames = mmgr.getClassesImplementingInterface(mmd.getTypeName(), clr);
}
} else {
String typeName = mmd.isSingleCollection() ? mmd.getCollection().getElementType() : mmd.getTypeName();
clsNames = new String[] { typeName };
}
DatastoreClass relatedTbl = storeMgr.getDatastoreClass(clsNames[0], clr);
JavaTypeMapping relatedMapping = relatedTbl.getMemberMapping(relatedMmd);
JavaTypeMapping relatedDiscrimMapping = relatedTbl.getSurrogateMapping(SurrogateColumnType.DISCRIMINATOR, true);
Object[] discrimValues = null;
JavaTypeMapping relatedTypeMapping = null;
AbstractClassMetaData relatedCmd = relatedMmd.getAbstractClassMetaData();
if (relatedDiscrimMapping != null && (relatedCmd.getSuperAbstractClassMetaData() != null || !relatedCmd.getFullClassName().equals(mmd.getTypeName()))) {
// Related table has a discriminator and the field can store other types
List discValueList = null;
for (String clsName : clsNames) {
List values = getDiscriminatorValuesForMember(clsName, relatedDiscrimMapping, storeMgr, clr);
if (discValueList == null) {
discValueList = values;
} else {
discValueList.addAll(values);
}
}
if (discValueList != null) {
discrimValues = discValueList.toArray(new Object[discValueList.size()]);
}
} else if (relatedTbl != relatedMapping.getTable()) {
// The relation is to a base class table, and the type stored is a sub-class
relatedTypeMapping = relatedTbl.getIdMapping();
}
SQLTable relatedSqlTbl = null;
if (relatedTypeMapping == null) {
// Join the 1-1 relation
JoinType joinType = getJoinTypeForOneToOneRelationJoin(sourceSqlTbl.getTable().getIdMapping(), sourceSqlTbl, inputJoinType);
if (joinType == JoinType.LEFT_OUTER_JOIN || joinType == JoinType.RIGHT_OUTER_JOIN) {
inputJoinType = joinType;
}
relatedSqlTbl = addJoinForOneToOneRelation(stmt, sourceSqlTbl.getTable().getIdMapping(), sourceSqlTbl, relatedMapping, relatedTbl, null, discrimValues, tableGroupName, joinType);
// Select the id mapping in the related table
int[] colNumbers = stmt.select(relatedSqlTbl, relatedTbl.getIdMapping(), null);
stmtMapping.setColumnPositions(colNumbers);
} else {
DatastoreClass relationTbl = (DatastoreClass) relatedMapping.getTable();
if (relatedTbl != relatedMapping.getTable()) {
if (relatedMapping.isNullable()) {
// Nullable - left outer join from {sourceTable}.ID to {relatedBaseTable}.FK
// and inner join from {relatedBaseTable}.ID to {relatedTable}.ID
// (joins the relation and restricts to the right type)
relatedSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sourceSqlTbl, sourceSqlTbl.getTable().getIdMapping(), relatedMapping.getTable(), null, relatedMapping, null, tableGroupName, true);
relatedSqlTbl = stmt.join(JoinType.INNER_JOIN, relatedSqlTbl, relatedMapping.getTable().getIdMapping(), relatedTbl, null, relatedTbl.getIdMapping(), null, tableGroupName, true);
} else {
// Not nullable - inner join from {sourceTable}.ID to {relatedBaseTable}.FK
// and inner join from {relatedBaseTable}.ID to {relatedTable}.ID
// (joins the relation and restricts to the right type)
relatedSqlTbl = stmt.join(JoinType.INNER_JOIN, sourceSqlTbl, sourceSqlTbl.getTable().getIdMapping(), relatedMapping.getTable(), null, relatedMapping, null, tableGroupName, true);
relatedSqlTbl = stmt.join(JoinType.INNER_JOIN, relatedSqlTbl, relatedMapping.getTable().getIdMapping(), relatedTbl, null, relatedTbl.getIdMapping(), null, tableGroupName, true);
}
} else {
// Join the 1-1 relation
JoinType joinType = getJoinTypeForOneToOneRelationJoin(sourceSqlTbl.getTable().getIdMapping(), sourceSqlTbl, inputJoinType);
if (joinType == JoinType.LEFT_OUTER_JOIN || joinType == JoinType.RIGHT_OUTER_JOIN) {
inputJoinType = joinType;
}
relatedSqlTbl = addJoinForOneToOneRelation(stmt, sourceSqlTbl.getTable().getIdMapping(), sourceSqlTbl, relatedMapping, relationTbl, null, null, tableGroupName, joinType);
}
// Select the id mapping in the subclass of the related table
// Note this adds an inner join from relatedTable to its subclass
relatedSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(stmt, relatedSqlTbl, relatedTbl.getIdMapping());
int[] colNumbers = stmt.select(relatedSqlTbl, relatedTbl.getIdMapping(), null);
stmtMapping.setColumnPositions(colNumbers);
}
if (selectSubobjects && !mmd.isSerialized() && !mmd.isEmbedded()) {
// Select the fetch-plan fields of the related object
StatementClassMapping subMappingDefinition = new StatementClassMapping(null, mmd.getName());
selectFetchPlanOfSourceClassInStatement(stmt, subMappingDefinition, fetchPlan, relatedSqlTbl, relatedMmd.getAbstractClassMetaData(), maxFetchPlanLimit - 1, inputJoinType);
if (mappingDefinition != null) {
mappingDefinition.addMappingDefinitionForMember(mmd.getAbsoluteFieldNumber(), subMappingDefinition);
}
}
} else if (relationType == RelationType.MANY_TO_ONE_BI) {
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
if (mmd.getJoinMetaData() != null || relatedMmds[0].getJoinMetaData() != null) {
// N-1 bidirectional join table relation
// Add left outer join from {sourceTable}.ID to {joinTable}.ELEM_FK
Table joinTable = storeMgr.getTable(relatedMmds[0]);
DatastoreElementContainer collTable = (DatastoreElementContainer) joinTable;
JavaTypeMapping selectMapping = collTable.getOwnerMapping();
SQLTable joinSqlTbl = null;
if (stmt.getPrimaryTable().getTable() != joinTable) {
// Join to the join table
JavaTypeMapping referenceMapping = collTable.getElementMapping();
if (referenceMapping instanceof ReferenceMapping) {
// Join table has a reference mapping pointing to our table, so get the submapping for the implementation
ReferenceMapping refMap = (ReferenceMapping) referenceMapping;
Class implType = clr.classForName(mmd.getClassName(true));
referenceMapping = refMap.getJavaTypeMappingForType(implType);
}
joinSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sourceSqlTbl, sourceSqlTbl.getTable().getIdMapping(), collTable, null, referenceMapping, null, tableGroupName + "_JOIN", true);
} else {
// Main table of the statement is the join table so no need to join
joinSqlTbl = stmt.getPrimaryTable();
}
// Select the owner mapping of the join table
int[] colNumbers = stmt.select(joinSqlTbl, selectMapping, null);
stmtMapping.setColumnPositions(colNumbers);
}
// TODO Select fetch plan fields of this related object
} else if (relationType == RelationType.MANY_TO_ONE_UNI) {
// Add left outer join from {sourceTable}.ID to {joinTable}.OWNER_FK
PersistableJoinTable joinTable = (PersistableJoinTable) storeMgr.getTable(mmd);
SQLTable joinSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sourceSqlTbl, sourceSqlTbl.getTable().getIdMapping(), joinTable, null, joinTable.getOwnerMapping(), null, tableGroupName + "_JOIN", true);
int[] colNumbers = stmt.select(joinSqlTbl, joinTable.getRelatedMapping(), null);
stmtMapping.setColumnPositions(colNumbers);
// TODO Select fetch plan fields of this related object
}
}
if (mappingDefinition != null) {
mappingDefinition.addMappingForMember(mmd.getAbsoluteFieldNumber(), stmtMapping);
}
}
}
use of org.datanucleus.metadata.RelationType in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method compileFromClassExpression.
/**
* Method to take a ClassExpression (in a FROM clause) and process the candidate and any
* linked JoinExpression(s), adding joins to the SQLStatement as required.
* @param clsExpr The ClassExpression
*/
protected void compileFromClassExpression(ClassExpression clsExpr) {
Symbol clsExprSym = clsExpr.getSymbol();
Class baseCls = (clsExprSym != null ? clsExprSym.getValueType() : null);
SQLTable candSqlTbl = stmt.getPrimaryTable();
MetaDataManager mmgr = storeMgr.getMetaDataManager();
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(baseCls, clr);
if (baseCls != null && !candidateAlias.equals(clsExpr.getAlias())) {
// Not candidate class so must be cross join (JPA spec 4.4.5)
DatastoreClass candTbl = storeMgr.getDatastoreClass(baseCls.getName(), clr);
candSqlTbl = stmt.join(JoinType.CROSS_JOIN, null, null, null, candTbl, clsExpr.getAlias(), null, null, null, null, true, null);
SQLTableMapping tblMapping = new SQLTableMapping(candSqlTbl, cmd, candTbl.getIdMapping());
setSQLTableMappingForAlias(clsExpr.getAlias(), tblMapping);
}
if (clsExpr.getCandidateExpression() != null && parentMapper != null) {
// User defined the candidate of the subquery as an implied join to the outer query
// e.g SELECT c FROM Customer c WHERE EXISTS (SELECT o FROM c.orders o ...)
// so add the join(s) to the outer query
processFromClauseSubquery(clsExpr, candSqlTbl, mmgr);
}
// Process all linked JoinExpression(s) for this ClassExpression
Expression rightExpr = clsExpr.getRight();
SQLTable sqlTbl = candSqlTbl;
JavaTypeMapping previousMapping = null;
while (rightExpr != null) {
if (rightExpr instanceof JoinExpression) {
JoinExpression joinExpr = (JoinExpression) rightExpr;
JoinExpression.JoinType exprJoinType = joinExpr.getType();
JoinType joinType = org.datanucleus.store.rdbms.sql.SQLJoin.getJoinTypeForJoinExpressionType(exprJoinType);
Expression joinedExpr = joinExpr.getJoinedExpression();
Expression joinOnExpr = joinExpr.getOnExpression();
String joinAlias = joinExpr.getAlias();
PrimaryExpression joinPrimExpr = null;
Class castCls = null;
if (joinedExpr instanceof PrimaryExpression) {
joinPrimExpr = (PrimaryExpression) joinedExpr;
} else if (joinedExpr instanceof DyadicExpression && joinedExpr.getOperator() == Expression.OP_CAST) {
// TREAT this join as a particular type. Cast type is processed below where we add the joins
joinPrimExpr = (PrimaryExpression) joinedExpr.getLeft();
String castClassName = (String) ((Literal) joinedExpr.getRight()).getLiteral();
castCls = clr.classForName(castClassName);
} else {
throw new NucleusException("We do not currently support JOIN to " + joinedExpr);
}
Iterator<String> iter = joinPrimExpr.getTuples().iterator();
String rootId = iter.next();
if (joinPrimExpr.getTuples().size() == 1 && !rootId.endsWith("#KEY") && !rootId.endsWith("#VALUE")) {
// DN Extension : Join to (new) root element? We need an ON expression to be supplied in this case
if (joinOnExpr == null) {
throw new NucleusUserException("Query has join to " + joinPrimExpr.getId() + " yet this is a root component and there is no ON expression");
}
// Add the basic join first with no condition since this root will be referenced in the "on" condition
baseCls = resolveClass(joinPrimExpr.getId());
DatastoreClass baseTbl = storeMgr.getDatastoreClass(baseCls.getName(), clr);
sqlTbl = stmt.join(joinType, candSqlTbl, baseTbl, joinAlias, null, null, true);
cmd = mmgr.getMetaDataForClass(baseCls, clr);
SQLTableMapping tblMapping = new SQLTableMapping(sqlTbl, cmd, baseTbl.getIdMapping());
setSQLTableMappingForAlias(joinAlias, tblMapping);
// Convert the ON expression to a BooleanExpression and add to the join
processingOnClause = true;
joinOnExpr.evaluate(this);
BooleanExpression joinOnSqlExpr = (BooleanExpression) stack.pop();
processingOnClause = false;
stmt.addAndConditionToJoinForTable(sqlTbl, joinOnSqlExpr, true);
// Move on to next join in the chain
rightExpr = rightExpr.getRight();
continue;
}
String joinTableGroupName = null;
SQLTable tblMappingSqlTbl = null;
JavaTypeMapping tblIdMapping = null;
AbstractMemberMetaData tblMmd = null;
boolean mapKey = false;
boolean mapValue = false;
String rootComponent = rootId;
if (rootComponent.endsWith("#KEY")) {
mapKey = true;
rootComponent = rootComponent.substring(0, rootComponent.length() - 4);
} else if (rootComponent.endsWith("#VALUE")) {
mapValue = true;
rootComponent = rootComponent.substring(0, rootComponent.length() - 6);
}
if (rootComponent.equalsIgnoreCase(candidateAlias)) {
// Join relative to the candidate
// Name table group of joined-to as per the relation
// Note : this will only work for one level out from the candidate TODO Extend this
cmd = candidateCmd;
joinTableGroupName = joinPrimExpr.getId();
sqlTbl = candSqlTbl;
} else {
// Join relative to some other alias
SQLTableMapping sqlTblMapping = getSQLTableMappingForAlias(rootComponent);
if (sqlTblMapping != null) {
if (sqlTblMapping.mmd != null && (mapKey || mapValue)) {
// First component is Map-related (i.e m#KEY, m#VALUE), so add any necessary join(s)
MapMetaData mapmd = sqlTblMapping.mmd.getMap();
cmd = mapKey ? mapmd.getKeyClassMetaData(clr) : mapmd.getValueClassMetaData(clr);
// Find the table forming the Map. This may be a join table, or the key or value depending on the type
// TODO Use OPTION_CASE_INSENSITIVE
sqlTbl = stmt.getTable(rootComponent + "_MAP");
if (sqlTbl == null) {
sqlTbl = stmt.getTable((rootComponent + "_MAP").toUpperCase());
if (sqlTbl == null) {
sqlTbl = stmt.getTable((rootComponent + "_MAP").toLowerCase());
}
}
String aliasForJoin = (iter.hasNext()) ? null : joinAlias;
boolean embedded = mapKey ? (mapmd.isEmbeddedKey() || mapmd.isSerializedKey()) : (mapmd.isEmbeddedValue() || mapmd.isSerializedValue());
if (mapmd.getMapType() == MapType.MAP_TYPE_JOIN) {
// Join from join table to KEY/VALUE as required
if (!embedded) {
if (mapKey) {
DatastoreClass keyTable = storeMgr.getDatastoreClass(mapmd.getKeyType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, ((MapTable) sqlTbl.getTable()).getKeyMapping(), keyTable, aliasForJoin, keyTable.getIdMapping(), null, joinTableGroupName, true);
} else {
DatastoreClass valueTable = storeMgr.getDatastoreClass(mapmd.getValueType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, ((MapTable) sqlTbl.getTable()).getValueMapping(), valueTable, aliasForJoin, valueTable.getIdMapping(), null, joinTableGroupName, true);
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
}
} else if (mapmd.getMapType() == MapType.MAP_TYPE_KEY_IN_VALUE) {
// TODO Cater for this type
} else if (mapmd.getMapType() == MapType.MAP_TYPE_VALUE_IN_KEY) {
// TODO Cater for this type
}
} else {
cmd = sqlTblMapping.cmd;
sqlTbl = sqlTblMapping.table;
}
joinTableGroupName = sqlTbl.getGroupName() + joinPrimExpr.getId().substring(rootComponent.length());
} else {
throw new NucleusUserException("Query has " + joinPrimExpr.getId() + " yet the first component " + rootComponent + " is unknown!");
}
}
while (iter.hasNext()) {
String id = iter.next();
String[] ids = id.contains(".") ? StringUtils.split(id, ".") : new String[] { id };
for (int k = 0; k < ids.length; k++) {
if (cmd == null) {
throw new NucleusUserException("Error in JOIN clause. id=" + id + " but component prior to " + ids[k] + " has no metadata");
}
boolean lastComponent = (k == ids.length - 1);
String thisComponent = ids[k];
mapKey = false;
mapValue = false;
if (thisComponent.endsWith("#KEY")) {
thisComponent = thisComponent.substring(0, thisComponent.length() - 4);
mapKey = true;
} else if (thisComponent.endsWith("#VALUE")) {
thisComponent = thisComponent.substring(0, thisComponent.length() - 6);
mapValue = true;
}
AbstractMemberMetaData mmd = cmd.getMetaDataForMember(thisComponent);
if (mmd == null) {
if (exprJoinType == JoinExpression.JoinType.JOIN_LEFT_OUTER || exprJoinType == JoinExpression.JoinType.JOIN_LEFT_OUTER_FETCH) {
// Polymorphic join, where the field exists in a subclass (doable since we have outer join)
String[] subclasses = mmgr.getSubclassesForClass(cmd.getFullClassName(), true);
for (int l = 0; l < subclasses.length; l++) {
AbstractClassMetaData subCmd = mmgr.getMetaDataForClass(subclasses[l], clr);
if (subCmd != null) {
mmd = subCmd.getMetaDataForMember(thisComponent);
if (mmd != null) {
cmd = subCmd;
break;
}
}
}
}
if (mmd == null) {
throw new NucleusUserException("Query has " + joinPrimExpr.getId() + " yet " + thisComponent + " is not found. Fix your input");
}
}
tblMmd = null;
String aliasForJoin = null;
if (k == (ids.length - 1) && !iter.hasNext()) {
aliasForJoin = joinAlias;
}
RelationType relationType = mmd.getRelationType(clr);
DatastoreClass relTable = null;
AbstractMemberMetaData relMmd = null;
if (relationType != RelationType.NONE) {
if (JoinExpression.JoinType.isFetch(exprJoinType)) {
// Add field to FetchPlan since marked for FETCH
String fgName = "QUERY_FETCH_" + mmd.getFullFieldName();
FetchGroupManager fetchGrpMgr = storeMgr.getNucleusContext().getFetchGroupManager();
if (fetchGrpMgr.getFetchGroupsWithName(fgName) == null) {
FetchGroup grp = new FetchGroup(storeMgr.getNucleusContext(), fgName, clr.classForName(cmd.getFullClassName()));
grp.addMember(mmd.getName());
fetchGrpMgr.addFetchGroup(grp);
}
fetchPlan.addGroup(fgName);
}
}
if (relationType == RelationType.ONE_TO_ONE_UNI) {
JavaTypeMapping otherMapping = null;
Object[] castDiscrimValues = null;
if (castCls != null && lastComponent) {
cmd = mmgr.getMetaDataForClass(castCls, clr);
if (cmd.hasDiscriminatorStrategy()) {
// Restrict discriminator on cast type to be the type+subclasses
castDiscrimValues = getDiscriminatorValuesForCastClass(cmd);
}
} else {
cmd = mmgr.getMetaDataForClass(mmd.getType(), clr);
}
if (mmd.isEmbedded()) {
// Embedded into the same table as before, so no join needed
otherMapping = sqlTbl.getTable().getMemberMapping(mmd);
} else {
if (sqlTbl.getTable() instanceof CollectionTable) {
// Currently in a join table, so work from the element and this being an embedded member
CollectionTable collTbl = (CollectionTable) sqlTbl.getTable();
JavaTypeMapping elemMapping = collTbl.getElementMapping();
if (elemMapping instanceof EmbeddedMapping) {
otherMapping = ((EmbeddedMapping) elemMapping).getJavaTypeMapping(mmd.getName());
}
} else {
otherMapping = sqlTbl.getTable().getMemberMapping(mmd);
}
relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
if (otherMapping == null && previousMapping != null) {
if (previousMapping instanceof EmbeddedMapping) {
// Part of an embedded 1-1 object, so find the relevant member mapping
EmbeddedMapping embMapping = (EmbeddedMapping) previousMapping;
otherMapping = embMapping.getJavaTypeMapping(mmd.getName());
}
}
if (otherMapping == null) {
// Polymorphic join? : cannot find this member in the candidate of the main statement, so need to pick which UNION
String tblGroupName = sqlTbl.getGroupName();
SQLTableGroup grp = stmt.getTableGroup(tblGroupName);
SQLTable nextSqlTbl = null;
// Try to find subtable in the same group that has a mapping for this member (and join from that)
SQLTable[] grpTbls = grp.getTables();
for (SQLTable grpTbl : grpTbls) {
if (grpTbl.getTable().getMemberMapping(mmd) != null) {
otherMapping = grpTbl.getTable().getMemberMapping(mmd);
break;
}
}
SQLTable newSqlTbl = stmt.join(joinType, sqlTbl, otherMapping, relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, false);
if (newSqlTbl != null) {
nextSqlTbl = newSqlTbl;
}
if (stmt instanceof SelectStatement) {
List<SelectStatement> unionStmts = ((SelectStatement) stmt).getUnions();
if (unionStmts != null) {
for (SQLStatement unionStmt : unionStmts) {
// Repeat the process for any unioned statements, find a subtable in the same group (and join from that)
otherMapping = null;
grp = unionStmt.getTableGroup(tblGroupName);
SQLTable[] unionGrpTbls = grp.getTables();
for (SQLTable grpTbl : unionGrpTbls) {
if (grpTbl.getTable().getMemberMapping(mmd) != null) {
otherMapping = grpTbl.getTable().getMemberMapping(mmd);
break;
}
}
newSqlTbl = unionStmt.join(joinType, sqlTbl, otherMapping, relTable, aliasForJoin, relTable.getIdMapping(), castDiscrimValues, joinTableGroupName, false);
if (newSqlTbl != null) {
nextSqlTbl = newSqlTbl;
}
}
}
}
sqlTbl = nextSqlTbl;
} else {
sqlTbl = stmt.join(joinType, sqlTbl, otherMapping, relTable, aliasForJoin, relTable.getIdMapping(), castDiscrimValues, joinTableGroupName, true);
}
}
previousMapping = otherMapping;
tblIdMapping = sqlTbl.getTable().getIdMapping();
tblMappingSqlTbl = sqlTbl;
} else if (relationType == RelationType.ONE_TO_ONE_BI) {
JavaTypeMapping otherMapping = null;
Object[] castDiscrimValues = null;
if (castCls != null && lastComponent) {
cmd = mmgr.getMetaDataForClass(castCls, clr);
if (cmd.hasDiscriminatorStrategy()) {
// Restrict discriminator on cast type to be the type+subclasses
castDiscrimValues = getDiscriminatorValuesForCastClass(cmd);
}
} else {
cmd = mmgr.getMetaDataForClass(mmd.getType(), clr);
}
if (mmd.isEmbedded()) {
// Embedded into the same table as before, so no join needed
otherMapping = sqlTbl.getTable().getMemberMapping(mmd);
} else {
relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
if (mmd.getMappedBy() != null) {
relMmd = mmd.getRelatedMemberMetaData(clr)[0];
JavaTypeMapping relMapping = relTable.getMemberMapping(relMmd);
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relTable, aliasForJoin, relMapping, castDiscrimValues, joinTableGroupName, true);
} else {
if (sqlTbl.getTable() instanceof CollectionTable) {
// Currently in a join table, so work from the element and this being an embedded member
CollectionTable collTbl = (CollectionTable) sqlTbl.getTable();
JavaTypeMapping elemMapping = collTbl.getElementMapping();
if (elemMapping instanceof EmbeddedMapping) {
otherMapping = ((EmbeddedMapping) elemMapping).getJavaTypeMapping(mmd.getName());
}
} else {
otherMapping = sqlTbl.getTable().getMemberMapping(mmd);
}
if (otherMapping == null && previousMapping != null) {
if (previousMapping instanceof EmbeddedMapping) {
// Part of an embedded 1-1 object, so find the relevant member mapping
EmbeddedMapping embMapping = (EmbeddedMapping) previousMapping;
otherMapping = embMapping.getJavaTypeMapping(mmd.getName());
}
}
sqlTbl = stmt.join(joinType, sqlTbl, otherMapping, relTable, aliasForJoin, relTable.getIdMapping(), castDiscrimValues, joinTableGroupName, true);
}
}
previousMapping = otherMapping;
tblIdMapping = sqlTbl.getTable().getIdMapping();
tblMappingSqlTbl = sqlTbl;
} else if (relationType == RelationType.ONE_TO_MANY_BI) {
previousMapping = null;
if (mmd.hasCollection()) {
// Join across COLLECTION relation
cmd = mmd.getCollection().getElementClassMetaData(clr);
if (mmd.getCollection().isEmbeddedElement() && mmd.getJoinMetaData() != null) {
// Embedded element stored in (collection) join table
CollectionTable relEmbTable = (CollectionTable) storeMgr.getTable(mmd);
JavaTypeMapping relOwnerMapping = relEmbTable.getOwnerMapping();
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relEmbTable, aliasForJoin, relOwnerMapping, null, joinTableGroupName, true);
tblMappingSqlTbl = sqlTbl;
tblIdMapping = relEmbTable.getElementMapping();
} else {
relTable = storeMgr.getDatastoreClass(mmd.getCollection().getElementType(), clr);
relMmd = mmd.getRelatedMemberMetaData(clr)[0];
if (mmd.getJoinMetaData() != null || relMmd.getJoinMetaData() != null) {
// Join to join table, then to related table
ElementContainerTable joinTbl = (ElementContainerTable) storeMgr.getTable(mmd);
SQLTable joinSqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, null, joinTbl.getOwnerMapping(), null, null, true);
sqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getElementMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
} else {
// Join to related table FK
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relTable, aliasForJoin, relTable.getMemberMapping(relMmd), null, joinTableGroupName, true);
}
tblIdMapping = sqlTbl.getTable().getIdMapping();
tblMappingSqlTbl = sqlTbl;
}
} else if (mmd.hasMap()) {
// Join across MAP relation
MapMetaData mapmd = mmd.getMap();
cmd = mapmd.getValueClassMetaData(clr);
tblMmd = mmd;
boolean embedded = mapKey ? (mapmd.isEmbeddedKey() || mapmd.isSerializedKey()) : (mapmd.isEmbeddedValue() || mapmd.isSerializedValue());
if (mapmd.getMapType() == MapType.MAP_TYPE_JOIN) {
// Add join to join table, then to related table (value)
MapTable joinTbl = (MapTable) storeMgr.getTable(mmd);
String aliasForMap = embedded ? aliasForJoin : (aliasForJoin + "_MAP");
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, aliasForMap, joinTbl.getOwnerMapping(), null, null, true);
if (embedded) {
tblMappingSqlTbl = sqlTbl;
tblIdMapping = mapKey ? joinTbl.getKeyMapping() : joinTbl.getValueMapping();
} else {
if (mapKey) {
// Join to key table and use that
relTable = storeMgr.getDatastoreClass(mapmd.getKeyType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, joinTbl.getKeyMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
} else {
// Join to value table and use that
relTable = storeMgr.getDatastoreClass(mapmd.getValueType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, joinTbl.getValueMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
}
} else if (mapmd.getMapType() == MapType.MAP_TYPE_KEY_IN_VALUE) {
// Join to value table
DatastoreClass valTable = storeMgr.getDatastoreClass(mapmd.getValueType(), clr);
JavaTypeMapping mapTblOwnerMapping;
if (mmd.getMappedBy() != null) {
mapTblOwnerMapping = valTable.getMemberMapping(mapmd.getValueClassMetaData(clr).getMetaDataForMember(mmd.getMappedBy()));
} else {
mapTblOwnerMapping = valTable.getExternalMapping(mmd, MappingType.EXTERNAL_FK);
}
String aliasForMap = (embedded || !mapKey) ? aliasForJoin : (aliasForJoin + "_MAP");
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), valTable, aliasForMap, mapTblOwnerMapping, null, null, true);
if (!embedded) {
if (mapKey) {
// Join to key table
JavaTypeMapping keyMapping = valTable.getMemberMapping(mmd.getKeyMetaData().getMappedBy());
relTable = storeMgr.getDatastoreClass(mapmd.getKeyType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, keyMapping, relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
}
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
} else if (mapmd.getMapType() == MapType.MAP_TYPE_VALUE_IN_KEY) {
// Join to key table, and then to value table
DatastoreClass keyTable = storeMgr.getDatastoreClass(mapmd.getKeyType(), clr);
JavaTypeMapping mapTblOwnerMapping;
if (mmd.getMappedBy() != null) {
mapTblOwnerMapping = keyTable.getMemberMapping(mapmd.getKeyClassMetaData(clr).getMetaDataForMember(mmd.getMappedBy()));
} else {
mapTblOwnerMapping = keyTable.getExternalMapping(mmd, MappingType.EXTERNAL_FK);
}
String aliasForMap = (embedded || mapKey) ? aliasForJoin : (aliasForJoin + "_MAP");
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), keyTable, aliasForMap, mapTblOwnerMapping, null, null, true);
if (!embedded) {
if (!mapKey) {
// Join to value table
JavaTypeMapping valueMapping = keyTable.getMemberMapping(mmd.getValueMetaData().getMappedBy());
relTable = storeMgr.getDatastoreClass(mapmd.getValueType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, valueMapping, relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
}
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
}
} else if (mmd.hasArray()) {
// Join across ARRAY relation
cmd = mmd.getArray().getElementClassMetaData(clr);
if (mmd.getArray().isEmbeddedElement() && mmd.getJoinMetaData() != null) {
// Embedded element stored in (array) join table
ArrayTable relEmbTable = (ArrayTable) storeMgr.getTable(mmd);
JavaTypeMapping relOwnerMapping = relEmbTable.getOwnerMapping();
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relEmbTable, aliasForJoin, relOwnerMapping, null, joinTableGroupName, true);
tblMappingSqlTbl = sqlTbl;
tblIdMapping = relEmbTable.getElementMapping();
} else {
relTable = storeMgr.getDatastoreClass(mmd.getArray().getElementType(), clr);
relMmd = mmd.getRelatedMemberMetaData(clr)[0];
if (mmd.getJoinMetaData() != null || relMmd.getJoinMetaData() != null) {
// Join to join table, then to related table
ElementContainerTable joinTbl = (ElementContainerTable) storeMgr.getTable(mmd);
SQLTable joinSqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, null, joinTbl.getOwnerMapping(), null, null, true);
sqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getElementMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
} else {
// Join to related table FK
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relTable, aliasForJoin, relTable.getMemberMapping(relMmd), null, joinTableGroupName, true);
}
tblIdMapping = sqlTbl.getTable().getIdMapping();
tblMappingSqlTbl = sqlTbl;
}
}
} else if (relationType == RelationType.ONE_TO_MANY_UNI) {
previousMapping = null;
if (mmd.hasCollection()) {
// Join across COLLECTION relation
cmd = mmd.getCollection().getElementClassMetaData(clr);
if (mmd.getCollection().isEmbeddedElement() && mmd.getJoinMetaData() != null) {
// Embedded element stored in (collection) join table
CollectionTable relEmbTable = (CollectionTable) storeMgr.getTable(mmd);
JavaTypeMapping relOwnerMapping = relEmbTable.getOwnerMapping();
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relEmbTable, aliasForJoin, relOwnerMapping, null, joinTableGroupName, true);
tblMappingSqlTbl = sqlTbl;
tblIdMapping = relEmbTable.getElementMapping();
} else {
relTable = storeMgr.getDatastoreClass(mmd.getCollection().getElementType(), clr);
if (mmd.getJoinMetaData() != null) {
// Join to join table, then to related table
ElementContainerTable joinTbl = (ElementContainerTable) storeMgr.getTable(mmd);
SQLTable joinSqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, null, joinTbl.getOwnerMapping(), null, null, true);
sqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getElementMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
} else {
// Join to related table FK
JavaTypeMapping relMapping = relTable.getExternalMapping(mmd, MappingType.EXTERNAL_FK);
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relTable, aliasForJoin, relMapping, null, joinTableGroupName, true);
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
}
} else if (mmd.hasMap()) {
// Join across MAP relation
MapMetaData mapmd = mmd.getMap();
cmd = mapmd.getValueClassMetaData(clr);
tblMmd = mmd;
boolean embedded = mapKey ? (mapmd.isEmbeddedKey() || mapmd.isSerializedKey()) : (mapmd.isEmbeddedValue() || mapmd.isSerializedValue());
if (mapmd.getMapType() == MapType.MAP_TYPE_JOIN) {
// Add join to join table, then to related table (value)
MapTable joinTbl = (MapTable) storeMgr.getTable(mmd);
String aliasForMap = (embedded || mapKey) ? aliasForJoin : (aliasForJoin + "_MAP");
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, aliasForMap, joinTbl.getOwnerMapping(), null, null, true);
if (embedded) {
tblMappingSqlTbl = sqlTbl;
tblIdMapping = mapKey ? joinTbl.getKeyMapping() : joinTbl.getValueMapping();
} else {
if (mapKey) {
// Join to key table and use that
relTable = storeMgr.getDatastoreClass(mapmd.getKeyType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, joinTbl.getKeyMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
} else {
// Join to value table and use that
relTable = storeMgr.getDatastoreClass(mapmd.getValueType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, joinTbl.getValueMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
}
} else if (mapmd.getMapType() == MapType.MAP_TYPE_KEY_IN_VALUE) {
// Join to value table
DatastoreClass valTable = storeMgr.getDatastoreClass(mapmd.getValueType(), clr);
JavaTypeMapping mapTblOwnerMapping;
if (mmd.getMappedBy() != null) {
mapTblOwnerMapping = valTable.getMemberMapping(mapmd.getValueClassMetaData(clr).getMetaDataForMember(mmd.getMappedBy()));
} else {
mapTblOwnerMapping = valTable.getExternalMapping(mmd, MappingType.EXTERNAL_FK);
}
String aliasForMap = (embedded || !mapKey) ? aliasForJoin : (aliasForJoin + "_MAP");
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), valTable, aliasForMap, mapTblOwnerMapping, null, null, true);
if (!embedded) {
if (mapKey) {
// Join to key table
JavaTypeMapping keyMapping = valTable.getMemberMapping(mmd.getKeyMetaData().getMappedBy());
relTable = storeMgr.getDatastoreClass(mapmd.getKeyType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, keyMapping, relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
}
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
} else if (mapmd.getMapType() == MapType.MAP_TYPE_VALUE_IN_KEY) {
// Join to key table, and then to value table
DatastoreClass keyTable = storeMgr.getDatastoreClass(mapmd.getKeyType(), clr);
JavaTypeMapping mapTblOwnerMapping;
if (mmd.getMappedBy() != null) {
mapTblOwnerMapping = keyTable.getMemberMapping(mapmd.getKeyClassMetaData(clr).getMetaDataForMember(mmd.getMappedBy()));
} else {
mapTblOwnerMapping = keyTable.getExternalMapping(mmd, MappingType.EXTERNAL_FK);
}
String aliasForMap = (embedded || mapKey) ? aliasForJoin : (aliasForJoin + "_MAP");
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), keyTable, aliasForMap, mapTblOwnerMapping, null, null, true);
if (!embedded) {
if (!mapKey) {
// Join to value table
JavaTypeMapping valueMapping = keyTable.getMemberMapping(mmd.getValueMetaData().getMappedBy());
relTable = storeMgr.getDatastoreClass(mapmd.getValueType(), clr);
sqlTbl = stmt.join(joinType, sqlTbl, valueMapping, relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
// TODO if there is an ON clause it needs to go on the correct join See [rdbms-177]
}
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
}
} else if (mmd.hasArray()) {
// Join across ARRAY relation
cmd = mmd.getArray().getElementClassMetaData(clr);
if (mmd.getArray().isEmbeddedElement() && mmd.getJoinMetaData() != null) {
// Embedded element stored in (array) join table
ArrayTable relEmbTable = (ArrayTable) storeMgr.getTable(mmd);
JavaTypeMapping relOwnerMapping = relEmbTable.getOwnerMapping();
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relEmbTable, aliasForJoin, relOwnerMapping, null, joinTableGroupName, true);
tblMappingSqlTbl = sqlTbl;
tblIdMapping = relEmbTable.getElementMapping();
} else {
relTable = storeMgr.getDatastoreClass(mmd.getArray().getElementType(), clr);
if (mmd.getJoinMetaData() != null) {
// Join to join table, then to related table
ElementContainerTable joinTbl = (ElementContainerTable) storeMgr.getTable(mmd);
SQLTable joinSqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, null, joinTbl.getOwnerMapping(), null, null, true);
sqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getElementMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
} else {
// Join to related table FK
JavaTypeMapping relMapping = relTable.getExternalMapping(mmd, MappingType.EXTERNAL_FK);
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), relTable, aliasForJoin, relMapping, null, joinTableGroupName, true);
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
}
}
} else if (relationType == RelationType.MANY_TO_MANY_BI) {
previousMapping = null;
relTable = storeMgr.getDatastoreClass(mmd.getCollection().getElementType(), clr);
cmd = mmd.getCollection().getElementClassMetaData(clr);
relMmd = mmd.getRelatedMemberMetaData(clr)[0];
// Join to join table, then to related table
if (mmd.hasCollection()) {
CollectionTable joinTbl = (CollectionTable) storeMgr.getTable(mmd);
SQLTable joinSqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, null, joinTbl.getOwnerMapping(), null, null, true);
sqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getElementMapping(), relTable, aliasForJoin, relTable.getIdMapping(), null, joinTableGroupName, true);
} else if (mmd.hasMap()) {
NucleusLogger.QUERY.warn("We do not support joining across a M-N MAP field : " + mmd.getFullFieldName());
} else if (mmd.hasArray()) {
NucleusLogger.QUERY.warn("We do not support joining across a M-N ARRAY field : " + mmd.getFullFieldName());
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
} else if (relationType == RelationType.MANY_TO_ONE_BI) {
previousMapping = null;
relTable = storeMgr.getDatastoreClass(mmd.getTypeName(), clr);
Object[] castDiscrimValues = null;
if (castCls != null && lastComponent) {
cmd = mmgr.getMetaDataForClass(castCls, clr);
if (cmd.hasDiscriminatorStrategy()) {
// Restrict discriminator on cast type to be the type+subclasses
castDiscrimValues = getDiscriminatorValuesForCastClass(cmd);
}
} else {
cmd = mmgr.getMetaDataForClass(mmd.getType(), clr);
}
relMmd = mmd.getRelatedMemberMetaData(clr)[0];
if (mmd.getJoinMetaData() != null || relMmd.getJoinMetaData() != null) {
// Join to join table, then to related table
if (mmd.hasCollection()) {
CollectionTable joinTbl = (CollectionTable) storeMgr.getTable(relMmd);
SQLTable joinSqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, null, joinTbl.getElementMapping(), null, null, true);
sqlTbl = stmt.join(joinType, joinSqlTbl, joinTbl.getOwnerMapping(), relTable, aliasForJoin, relTable.getIdMapping(), castDiscrimValues, joinTableGroupName, true);
} else if (mmd.hasMap()) {
NucleusLogger.QUERY.warn("We do not support joining across a N-1 MAP field : " + mmd.getFullFieldName());
} else if (mmd.hasArray()) {
NucleusLogger.QUERY.warn("We do not support joining across a N-1 ARRAY field : " + mmd.getFullFieldName());
}
} else {
// Join to owner table
JavaTypeMapping fkMapping = sqlTbl.getTable().getMemberMapping(mmd);
sqlTbl = stmt.join(joinType, sqlTbl, fkMapping, relTable, aliasForJoin, relTable.getIdMapping(), castDiscrimValues, joinTableGroupName, true);
}
tblMappingSqlTbl = sqlTbl;
tblIdMapping = tblMappingSqlTbl.getTable().getIdMapping();
} else {
// NO RELATION, but cater for join table cases
previousMapping = null;
if (mmd.hasCollection()) {
cmd = null;
if (mmd.getJoinMetaData() != null) {
// Join to join table
CollectionTable joinTbl = (CollectionTable) storeMgr.getTable(mmd);
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, aliasForJoin, joinTbl.getOwnerMapping(), null, null, true);
tblMappingSqlTbl = sqlTbl;
tblIdMapping = joinTbl.getElementMapping();
} else {
throw new NucleusUserException("FROM clause contains join to Collection field at " + mmd.getFullFieldName() + " yet this has no join table");
}
} else if (mmd.hasMap()) {
MapMetaData mapmd = mmd.getMap();
cmd = mapmd.getValueClassMetaData(clr);
tblMmd = mmd;
if (// Should be the only type when relationType is NONE
mapmd.getMapType() == MapType.MAP_TYPE_JOIN) {
// Add join to join table
MapTable joinTbl = (MapTable) storeMgr.getTable(mmd);
String aliasForMap = aliasForJoin;
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, aliasForMap, joinTbl.getOwnerMapping(), null, null, true);
tblMappingSqlTbl = sqlTbl;
tblIdMapping = joinTbl.getValueMapping();
} else {
throw new NucleusUserException("FROM clause contains join to Map field at " + mmd.getFullFieldName() + " yet this has no join table");
}
} else if (mmd.hasArray()) {
cmd = null;
if (mmd.getJoinMetaData() != null) {
// Join to join table
ArrayTable joinTbl = (ArrayTable) storeMgr.getTable(mmd);
sqlTbl = stmt.join(joinType, sqlTbl, sqlTbl.getTable().getIdMapping(), joinTbl, aliasForJoin, joinTbl.getOwnerMapping(), null, null, true);
tblMappingSqlTbl = sqlTbl;
tblIdMapping = joinTbl.getElementMapping();
} else {
throw new NucleusUserException("FROM clause contains join to array field at " + mmd.getFullFieldName() + " yet this has no join table");
}
}
}
}
}
if (joinAlias != null) {
if (explicitJoinPrimaryByAlias == null) {
explicitJoinPrimaryByAlias = new HashMap<>();
}
explicitJoinPrimaryByAlias.put(joinAlias, joinPrimExpr.getId());
SQLTableMapping tblMapping = null;
if (tblMmd != null) {
// Maps store the member so we can more easily navigate to the key/value
tblMapping = new SQLTableMapping(tblMappingSqlTbl, cmd, tblMmd, tblIdMapping);
} else {
tblMapping = new SQLTableMapping(tblMappingSqlTbl, cmd, tblIdMapping);
}
setSQLTableMappingForAlias(joinAlias, tblMapping);
}
if (joinOnExpr != null) {
// Convert the ON expression to a BooleanExpression
processingOnClause = true;
joinOnExpr.evaluate(this);
BooleanExpression joinOnSqlExpr = (BooleanExpression) stack.pop();
processingOnClause = false;
// Add the ON expression to the most recent SQLTable at the end of this chain
// TODO Allow for SQL JOIN "grouping" [rdbms-177]. This applies to all cases where we join to a join table then to an element/value table and
// need to apply the ON clause across both
SQLJoin join = stmt.getJoinForTable(sqlTbl);
join.addAndCondition(joinOnSqlExpr);
}
} else {
previousMapping = null;
}
// Move on to next join in the chain
rightExpr = rightExpr.getRight();
}
}
use of org.datanucleus.metadata.RelationType in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method prepareDatastoreMapping.
/**
* Method to prepare the PC mapping and add its associated datastore mappings.
* @param clr The ClassLoaderResolver
*/
protected void prepareDatastoreMapping(ClassLoaderResolver clr) {
if (roleForMember == FieldRole.ROLE_COLLECTION_ELEMENT) {
// TODO Handle creation of columns in join table for collection of PCs
} else if (roleForMember == FieldRole.ROLE_ARRAY_ELEMENT) {
// TODO Handle creation of columns in join table for array of PCs
} else if (roleForMember == FieldRole.ROLE_MAP_KEY) {
// TODO Handle creation of columns in join table for map of PCs as keys
} else if (roleForMember == FieldRole.ROLE_MAP_VALUE) {
// TODO Handle creation of columns in join table for map of PCs as values
} else {
// Either one end of a 1-1 relation, or the N end of a N-1
AbstractClassMetaData refCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
JavaTypeMapping referenceMapping = null;
if (refCmd == null) {
// User stupidity
throw new NucleusUserException("You have a field " + mmd.getFullFieldName() + " that has type " + mmd.getTypeName() + " but this type has no known metadata. Your mapping is incorrect");
}
if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
// Find the actual tables storing the other end (can be multiple subclasses)
AbstractClassMetaData[] cmds = storeMgr.getClassesManagingTableForClass(refCmd, clr);
if (cmds != null && cmds.length > 0) {
if (cmds.length > 1) {
// TODO Only log this when it is really necessary. In some situations it is fine
NucleusLogger.PERSISTENCE.warn("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"subclass-table\" inheritance strategy and more " + "than 1 subclasses with a table. This is not fully supported");
}
} else {
// TODO Throw an exception ?
return;
}
// TODO We need a mapping for each of the possible subclass tables
referenceMapping = storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIdMapping();
} else if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
// Find the other side of the relation
DatastoreClass refTable = null;
if (refCmd instanceof ClassMetaData && !((ClassMetaData) refCmd).isAbstract()) {
refTable = storeMgr.getDatastoreClass(refCmd.getFullClassName(), clr);
} else {
Collection<String> refSubclasses = storeMgr.getSubClassesForClass(refCmd.getFullClassName(), true, clr);
if (refSubclasses != null && !refSubclasses.isEmpty()) {
// if only 1 subclass then use that
String refSubclassName = refSubclasses.iterator().next();
refTable = storeMgr.getDatastoreClass(refSubclassName, clr);
if (refSubclasses.size() > 1) {
NucleusLogger.DATASTORE_SCHEMA.info("Field " + mmd.getFullFieldName() + " is a 1-1/N-1 relation and the other side had multiple possible classes " + "to which to create a foreign-key. Using first possible (" + refSubclassName + ")");
}
}
}
if (refTable != null) {
referenceMapping = refTable.getIdMapping();
} else {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"complete-table\" inheritance strategy and either no table was found, or multiple possible tables!");
}
} else {
// Default is to use the ID of the related object
// TODO Add option to use a natural-id in the other class. Find the mapping using the targetColumnName
referenceMapping = storeMgr.getDatastoreClass(mmd.getType().getName(), clr).getIdMapping();
}
// Generate a mapping from the columns of the referenced object to this mapping's ColumnMetaData
CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(mmd, table, referenceMapping, true);
// Find any related field where this is part of a bidirectional relation
RelationType relationType = mmd.getRelationType(clr);
boolean createDatastoreMappings = true;
if (relationType == RelationType.MANY_TO_ONE_BI) {
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than 1 related field
createDatastoreMappings = (relatedMmds[0].getJoinMetaData() == null);
} else if (// TODO If join table then don't need this
relationType == RelationType.ONE_TO_ONE_BI) {
// Put the FK at the end without "mapped-by"
createDatastoreMappings = (mmd.getMappedBy() == null);
}
if (mmd.getJoinMetaData() != null && (relationType == RelationType.MANY_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI)) {
if (relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI) {
throw new NucleusUserException("We do not currently support 1-1 relations via join table : " + mmd.getFullFieldName());
}
// create join table
storeMgr.newJoinTable(table, mmd, clr);
} else {
// Loop through the datastore fields in the referenced class and create a datastore field for each
for (int i = 0; i < referenceMapping.getNumberOfDatastoreMappings(); i++) {
DatastoreMapping refDatastoreMapping = referenceMapping.getDatastoreMapping(i);
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(refDatastoreMapping.getJavaTypeMapping().getJavaType());
this.addJavaTypeMapping(mapping);
// Create physical datastore columns where we require a FK link to the related table.
if (createDatastoreMappings) {
// Find the Column MetaData that maps to the referenced datastore field
ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(refDatastoreMapping.getColumn().getIdentifier());
if (colmd == null) {
throw new NucleusUserException(Localiser.msg("041038", refDatastoreMapping.getColumn().getIdentifier(), toString())).setFatal();
}
// Create a Datastore field to equate to the referenced classes datastore field
MappingManager mmgr = storeMgr.getMappingManager();
Column col = mmgr.createColumn(mmd, table, mapping, colmd, refDatastoreMapping.getColumn(), clr);
// Add its datastore mapping
DatastoreMapping datastoreMapping = mmgr.createDatastoreMapping(mapping, col, refDatastoreMapping.getJavaTypeMapping().getJavaTypeForDatastoreMapping(i));
this.addDatastoreMapping(datastoreMapping);
} else {
mapping.setReferenceMapping(referenceMapping);
}
}
}
}
}
use of org.datanucleus.metadata.RelationType in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method preDelete.
/**
* Method executed just before the owning object is deleted, allowing tidying up of any relation information.
* @param op ObjectProvider for the owner
*/
public void preDelete(ObjectProvider op) {
int fieldNumber = mmd.getAbsoluteFieldNumber();
if (!op.isFieldLoaded(fieldNumber)) {
// makes sure field is loaded
try {
op.loadField(fieldNumber);
} catch (NucleusObjectNotFoundException onfe) {
// Already deleted so just return
return;
}
}
Object pc = op.provideField(fieldNumber);
pc = mmd.isSingleCollection() ? SCOUtils.singleCollectionValue(getStoreManager().getNucleusContext().getTypeManager(), pc) : pc;
if (pc == null) {
// Null value so nothing to do
return;
}
ExecutionContext ec = op.getExecutionContext();
ClassLoaderResolver clr = ec.getClassLoaderResolver();
// N-1 Uni, so delete join table entry
RelationType relationType = mmd.getRelationType(clr);
if (relationType == RelationType.MANY_TO_ONE_UNI) {
// Update join table entry
PersistableRelationStore store = (PersistableRelationStore) storeMgr.getBackingStoreForField(clr, mmd, mmd.getType());
store.remove(op);
}
// Check if we should delete the related object when this object is deleted
boolean dependent = mmd.isDependent();
if (mmd.isCascadeRemoveOrphans()) {
// JPA allows "orphan removal" to define deletion of the other side
dependent = true;
}
// Check if the field has a FK defined
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than 1 related field
boolean hasFK = false;
if (!dependent) {
// Not dependent, so check if the datastore has a FK and will take care of it for us
if (mmd.getForeignKeyMetaData() != null) {
hasFK = true;
}
if (RelationType.isBidirectional(relationType) && relatedMmds[0].getForeignKeyMetaData() != null) {
hasFK = true;
}
if (ec.getStringProperty(PropertyNames.PROPERTY_DELETION_POLICY).equals("JDO2")) {
// JDO doesn't currently take note of foreign-key
hasFK = false;
}
}
// There may be some corner cases that this code doesn't yet cater for
if (relationType == RelationType.ONE_TO_ONE_UNI || (relationType == RelationType.ONE_TO_ONE_BI && mmd.getMappedBy() == null)) {
// 1-1 with FK at this side (owner of the relation)
if (dependent) {
boolean relatedObjectDeleted = ec.getApiAdapter().isDeleted(pc);
if (isNullable() && !relatedObjectDeleted) {
// Other object not yet deleted, but the field is nullable so just null out the FK
// TODO Not doing this would cause errors in 1-1 uni relations (e.g AttachDetachTest)
// TODO Log this since it affects the resultant objects
op.replaceFieldMakeDirty(fieldNumber, null);
storeMgr.getPersistenceHandler().updateObject(op, new int[] { fieldNumber });
if (!relatedObjectDeleted) {
// Mark the other object for deletion since not yet tagged
ec.deleteObjectInternal(pc);
}
} else {
// Can't just delete the other object since that would cause a FK constraint violation. Do nothing - handled by DeleteRequest on other object
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.DATASTORE_PERSIST.debug(Localiser.msg("041017", StringUtils.toJVMIDString(op.getObject()), mmd.getFullFieldName()));
}
}
} else {
// We're deleting the FK at this side so shouldn't be an issue
AbstractMemberMetaData relatedMmd = mmd.getRelatedMemberMetaDataForObject(clr, op.getObject(), pc);
if (relatedMmd != null) {
ObjectProvider otherOP = ec.findObjectProvider(pc);
if (otherOP != null) {
// Managed Relations : 1-1 bidir, so null out the object at the other
Object currentValue = otherOP.provideField(relatedMmd.getAbsoluteFieldNumber());
if (currentValue != null) {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("041019", StringUtils.toJVMIDString(pc), relatedMmd.getFullFieldName(), op.getObjectAsPrintable()));
}
otherOP.replaceFieldMakeDirty(relatedMmd.getAbsoluteFieldNumber(), null);
if (ec.getManageRelations()) {
otherOP.getExecutionContext().getRelationshipManager(otherOP).relationChange(relatedMmd.getAbsoluteFieldNumber(), op.getObject(), null);
}
}
}
}
}
} else if (relationType == RelationType.ONE_TO_ONE_BI && mmd.getMappedBy() != null) {
// 1-1 with FK at other side
DatastoreClass relatedTable = storeMgr.getDatastoreClass(relatedMmds[0].getClassName(), clr);
JavaTypeMapping relatedMapping = relatedTable.getMemberMapping(relatedMmds[0]);
boolean isNullable = relatedMapping.isNullable();
ObjectProvider otherOP = ec.findObjectProvider(pc);
if (dependent) {
if (isNullable) {
// Null out the FK in the datastore using a direct update (since we are deleting)
otherOP.replaceFieldMakeDirty(relatedMmds[0].getAbsoluteFieldNumber(), null);
storeMgr.getPersistenceHandler().updateObject(otherOP, new int[] { relatedMmds[0].getAbsoluteFieldNumber() });
}
// Mark the other object for deletion
ec.deleteObjectInternal(pc);
} else if (!hasFK) {
if (isNullable()) {
Object currentRelatedValue = otherOP.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (currentRelatedValue != null) {
// Null out the FK in the datastore using a direct update (since we are deleting)
otherOP.replaceFieldMakeDirty(relatedMmds[0].getAbsoluteFieldNumber(), null);
storeMgr.getPersistenceHandler().updateObject(otherOP, new int[] { relatedMmds[0].getAbsoluteFieldNumber() });
// Managed Relations : 1-1 bidir, so null out the object at the other
if (ec.getManageRelations()) {
otherOP.getExecutionContext().getRelationshipManager(otherOP).relationChange(relatedMmds[0].getAbsoluteFieldNumber(), op.getObject(), null);
}
}
} else {
// TODO Remove it
}
} else {
// User has a FK defined (in MetaData) so let the datastore take care of it
}
} else if (relationType == RelationType.MANY_TO_ONE_BI) {
ObjectProvider otherOP = ec.findObjectProvider(pc);
if (relatedMmds[0].getJoinMetaData() == null) {
// N-1 with FK at this side
if (otherOP.isDeleting()) {
// Other object is being deleted too but this side has the FK so just delete this object
} else {
// Other object is not being deleted so delete it if necessary
if (dependent) {
if (isNullable()) {
// TODO Datastore nullability info can be unreliable so try to avoid this call
// Null out the FK in the datastore using a direct update (since we are deleting)
op.replaceFieldMakeDirty(fieldNumber, null);
storeMgr.getPersistenceHandler().updateObject(op, new int[] { fieldNumber });
}
if (ec.getApiAdapter().isDeleted(pc)) {
// Object is already tagged for deletion but we're deleting the FK so leave til flush()
} else {
// Mark the other object for deletion
ec.deleteObjectInternal(pc);
}
} else {
// Managed Relations : remove element from collection/map
if (relatedMmds[0].hasCollection()) {
// Only update the other side if not already being deleted
if (!ec.getApiAdapter().isDeleted(otherOP.getObject()) && !otherOP.isDeleting()) {
// Make sure the other object is updated in any caches
ec.markDirty(otherOP, false);
// Make sure collection field is loaded
otherOP.isLoaded(relatedMmds[0].getAbsoluteFieldNumber());
Collection otherColl = (Collection) otherOP.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (otherColl != null) {
if (ec.getManageRelations()) {
otherOP.getExecutionContext().getRelationshipManager(otherOP).relationRemove(relatedMmds[0].getAbsoluteFieldNumber(), op.getObject());
}
// TODO Localise this message
NucleusLogger.PERSISTENCE.debug("ManagedRelationships : delete of object causes removal from collection at " + relatedMmds[0].getFullFieldName());
otherColl.remove(op.getObject());
}
}
} else if (relatedMmds[0].hasMap()) {
// TODO Cater for maps, but what is the key/value pair ?
}
}
}
} else {
// N-1 with join table so no FK here so need to remove from Collection/Map first? (managed relations)
if (dependent) {
// Mark the other object for deletion
ec.deleteObjectInternal(pc);
} else {
// Managed Relations : remove element from collection/map
if (relatedMmds[0].hasCollection()) {
// Only update the other side if not already being deleted
if (!ec.getApiAdapter().isDeleted(otherOP.getObject()) && !otherOP.isDeleting()) {
// Make sure the other object is updated in any caches
ec.markDirty(otherOP, false);
// Make sure the other object has the collection loaded so does this change
otherOP.isLoaded(relatedMmds[0].getAbsoluteFieldNumber());
Collection otherColl = (Collection) otherOP.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (otherColl != null) {
// TODO Localise this
NucleusLogger.PERSISTENCE.debug("ManagedRelationships : delete of object causes removal from collection at " + relatedMmds[0].getFullFieldName());
otherColl.remove(op.getObject());
}
}
} else if (relatedMmds[0].hasMap()) {
// TODO Cater for maps, but what is the key/value pair ?
}
}
}
} else if (relationType == RelationType.MANY_TO_ONE_UNI) {
// N-1 uni with join table
if (dependent) {
// Mark the other object for deletion
ec.deleteObjectInternal(pc);
}
} else {
// No relation so what is this field ?
}
}
use of org.datanucleus.metadata.RelationType in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method postUpdate.
/**
* Method executed just afer any update of the owning object, allowing any necessary action to this field and the object stored in it.
* @param op ObjectProvider for the owner
*/
public void postUpdate(ObjectProvider op) {
Object pc = op.provideField(mmd.getAbsoluteFieldNumber());
pc = mmd.isSingleCollection() ? SCOUtils.singleCollectionValue(getStoreManager().getNucleusContext().getTypeManager(), pc) : pc;
ClassLoaderResolver clr = op.getExecutionContext().getClassLoaderResolver();
RelationType relationType = mmd.getRelationType(clr);
if (pc == null) {
if (relationType == RelationType.MANY_TO_ONE_UNI) {
// Update join table entry
PersistableRelationStore store = (PersistableRelationStore) storeMgr.getBackingStoreForField(clr, mmd, mmd.getType());
store.remove(op);
}
return;
}
ObjectProvider otherOP = op.getExecutionContext().findObjectProvider(pc);
if (otherOP == null) {
if (relationType == RelationType.ONE_TO_ONE_BI || relationType == RelationType.MANY_TO_ONE_BI || relationType == RelationType.MANY_TO_ONE_UNI) {
// Related object is not yet persisted (e.g 1-1 with FK at other side) so persist it
Object other = op.getExecutionContext().persistObjectInternal(pc, null, -1, ObjectProvider.PC);
otherOP = op.getExecutionContext().findObjectProvider(other);
}
}
if (relationType == RelationType.MANY_TO_ONE_UNI) {
// Update join table entry
PersistableRelationStore store = (PersistableRelationStore) storeMgr.getBackingStoreForField(clr, mmd, mmd.getType());
store.update(op, otherOP);
}
}
Aggregations