* @return The QueryStatement that will return the instances
*/
public QueryExpression newQueryStatement(Class candidateClass, DatastoreIdentifier candidateAlias)
{
final ClassLoaderResolver clr = om.getClassLoaderResolver();
QueryExpression query = null;
if (tables == null)
{
return null;
}
// In the multiple table case we pass in the class of the table rather than the candidate class since
// the instances of that table can only be of the tables type ("subclass-table" strategy)
for (int i = 0; i < tables.length; i++)
{
final int tableNo = i;
// Load using the class loader of the candidate class where possible - JDO spec 12.5
Class cls = null;
try
{
cls = clr.classForName(tables[tableNo].getType(), this.candidateClass.getClassLoader());
}
catch (ClassNotResolvedException cnfe)
{
throw new JPOXUserException(LOCALISER_RDBMS.msg("053002",
candidateClass.getName(), tables[tableNo].getType())).setFatal();
}
if (queryUsingDiscriminator())
{
// Use a single select with discrim since all in 1 table, and select the discrim column for identification
Class[] candidates = null;
if (storeMgr.getOMFContext().getTypeManager().isReferenceType(candidateClass))
{
// Take the metadata for the first implementation of the reference type
String[] clsNames = storeMgr.getOMFContext().getMetaDataManager().getClassesImplementingInterface(candidateClass.getName(), clr);
candidates = new Class[clsNames.length];
for (int j=0; j<clsNames.length; j++)
{
candidates[j] = clr.classForName(clsNames[j]);
}
}
else
{
candidates = new Class[] {candidateClass};
}
query = (new DiscriminatorIteratorStatement(clr,
candidates, subclasses, storeMgr, true)).getQueryStatement(candidateAlias);
}
else
{
// Use a UNION since we need to join across multiple tables
boolean completeTableInheritance =
cmd.getInheritanceMetaData().getStrategyValue() == InheritanceStrategy.COMPLETE_TABLE;
boolean useSubclassesInStatement = subclasses;
if (completeTableInheritance)
{
// "complete-table" so ignore subclasses since each table is just that class
useSubclassesInStatement = false;
}
QueryExpression query_table = new UnionIteratorStatement(clr,
multipleTableCase ? cls : candidateClass, useSubclassesInStatement, storeMgr,
clr.classForName(tables[tableNo].getType()),
tables[tableNo].getIDMapping(), tables[tableNo], false, Boolean.TRUE,
!completeTableInheritance, false).getQueryStatement(candidateAlias);