I am starting to use code generation with NHibernate and it is pretty awesome. I created my own renderer and everything was cooking along fine. I use my own meta tags etc... to generate my classes.
One of my mappings uses the option to have the subclass element be the first element in the mapping file to create the class mapping. It seemed that the code generator just ignored this class.
After some digging I found this line
Code:
ClassMapping superclass = (ClassMapping) allMaps[child.SuperClass];
in the FindParents(HashTable) method of CodeGenerator.cs to be the problem. Since my base class is a sub-class of another class, it will not be found by this method.
So, I created two new methods that will search the object tree recursivly.
Code:
private static ClassMapping FindClass(string className)
{
try
{
ClassMapping returnValue = null;
foreach (ClassMapping mapping in allMaps.Values)
{
returnValue = FindClass(className, mapping);
if (returnValue != null)
break;
}
return returnValue;
}
catch (Exception)
{
throw;
}
}
private static ClassMapping FindClass(string className, ClassMapping classMapping)
{
try
{
ClassMapping returnValue = null;
if (classMapping.ClassName.FullyQualifiedName == className)
returnValue = classMapping;
else
{
foreach (ClassMapping mapping in classMapping.Subclasses)
{
returnValue = FindClass(className, mapping);
if (returnValue != null)
break;
}
}
return returnValue;
}
catch (Exception)
{
throw;
}
}
then I changed the following line:
Code:
ClassMapping superclass = (ClassMapping) allMaps[child.SuperClass];
to
Code:
ClassMapping superclass = FindClass(child.SuperClass);
This is for the event that you have this heirarchy:
ClassC: ClassB: ClassA
ClassA & ClassB are defined in one mapping file and ClassC is in another mapping file.