}
private EntityResult compileEntityResult(EJBQLExpression expression, int position) {
String id = expression.getText().toLowerCase();
ClassDescriptor descriptor = descriptorsById.get(id);
final EntityResult entityResult = new EntityResult(descriptor.getObjectClass());
final String prefix = "ec" + position + "_";
final int[] index = {
0
};
final Set<String> visited = new HashSet<String>();
PropertyVisitor visitor = new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
ObjAttribute oa = property.getAttribute();
if (visited.add(oa.getDbAttributePath())) {
entityResult.addObjectField(
oa.getEntity().getName(),
oa.getName(),
prefix + index[0]++);
}
return true;
}
public boolean visitToMany(ToManyProperty property) {
return true;
}
public boolean visitToOne(ToOneProperty property) {
ObjRelationship rel = property.getRelationship();
DbRelationship dbRel = rel.getDbRelationships().get(0);
for (DbJoin join : dbRel.getJoins()) {
DbAttribute src = join.getSource();
if (src.isForeignKey() && visited.add(src.getName())) {
entityResult.addDbField(src.getName(), prefix + index[0]++);
}
}
return true;
}
};
// EJBQL queries are polymorphic by definition - there is no distinction between
// inheritance/no-inheritance fetch
descriptor.visitAllProperties(visitor);
// append id columns ... (some may have been appended already via relationships)
DbEntity table = descriptor.getEntity().getDbEntity();
for (DbAttribute pk : table.getPrimaryKeys()) {
if (visited.add(pk.getName())) {
entityResult.addDbField(pk.getName(), prefix + index[0]++);
}
}
// append inheritance discriminator columns...
Iterator<DbAttribute> discriminatorColumns = descriptor.getDiscriminatorColumns();
while (discriminatorColumns.hasNext()) {
DbAttribute column = discriminatorColumns.next();
if (visited.add(column.getName())) {
entityResult.addDbField(column.getName(), prefix + index[0]++);
}
}
return entityResult;
}