grails/grails-data-mapping — pull request #10 by domurtag (2014-02-07 20:31:14+00:00)
Title: fixes grails-7729

I couldn't find a way to write a unit/integration test for this change, so here's how I tested it manually:

Create a new project and add these domain classes

````groovy
class Parent {
static mapping = {
tablePerHierarchy false
}
}

class Child extends Parent {
}
````
When you generate the schema there will be no foreign key relationship between Child and Parent. I then created the following class (which changes GrailsAnnotationConfiguration in an equivalent way to this PR):
````java
public class TablePerSubclassConfiguration extends GrailsAnnotationConfiguration {

private static final long serialVersionUID = 1;
private boolean alreadyProcessed = false;

@Override
protected void secondPassCompile() throws MappingException {
super.secondPassCompile();

if (alreadyProcessed) {
return;
}

for (PersistentClass persistentClass : classes.values()) {
if (persistentClass instanceof RootClass) {
RootClass rootClass = (RootClass) persistentClass;

if (rootClass.hasSubclasses()) {
Iterator subclasses = rootClass.getSubclassIterator();

while (subclasses.hasNext()) {

Object subclass = subclasses.next();

// This test ensures that foreign keys will only be created for subclasses that are
// mapped using "table per subclass"
if (subclass instanceof JoinedSubclass) {
JoinedSubclass joinedSubclass = (JoinedSubclass) subclass;
joinedSubclass.createForeignKey();
}
}
}
}
}

alreadyProcessed = true;
}
}
````
and configured this class to be used in `DataSource.groovy`

````groovy
dataSource {
configClass = 'com.ecmhp.domain.gorm.TablePerSubclassConfiguration'
// other datasource config goes here
}
````
Now when the schema is generated the foreign keys are created. I also added parent and child domain classes that use `tablePerHierarchy true` mapping to check that that they still work as before (and they do).