Mapping inheritance objects
I have two tables in the database one called Person and one called Faculty. I also have the corresponding classes in code. Faculty inherits from Person.
In the database the primary key for the Person table is personId. The Faculty table also has a personId field which is a foreign key to the Person table.
I have been able to map the Person classt correctly without the Faculty class and got it to work.
public class PersonMap : IAutoMappingOverride
{
public void Override(AutoMapping<Person> mapping)
{
mapping.Id(x =>x.Id).Column("personId").GeneratedBy.Assigned();
}
}
When I run the code the SQL that is generated from NHibernate is looking for PersonFK in the Faculty table, which of course is not there.
I am struggling with figuring out how to tell it that Person and Faculty are joined on personId and the name of the field is "personId".
I tried something like this but it did not work.
public class FacultyMap : IAutoMappingOverride
{
public void Override(AutoMapping<Faculty> mapping)
{
mapping.JoinedSubClass<Faculty>("personId");
}
}
It seems like something simple to do and I am sure there is a way to do it, I just don't know how. Any help is greatly appreciated
2 Posted by Ilir Lako on 02 Jul, 2010 06:46 PM
Anybody who can help with this?
Support Staff 3 Posted by Paul Batum on 05 Jul, 2010 12:12 PM
I forget how this works exactly but I think you are really close. Try:
public class PersonMap : IAutoMappingOverride<Person>
{
public void Override(AutoMapping<Person> mapping)
{
mapping.Id(x =>x.Id).Column("personId").GeneratedBy.Assigned();
mapping.JoinedSubClass<Faculty>("personId");
}
}
Let me know if it doesn't work - I'll sit down and figure it out properly.
4 Posted by Ilir Lako on 08 Jul, 2010 07:09 PM
Hi Paul,
I tried your suggestion and it did not work. I am still getting the error "Invalid column name 'PersonFk'." Somehow it is still looking for a PersonFk column and the tables are just set to be related on the personId field.
I would greatly appreciate it if you could look more into it. I have tried everything that I could think of and could find out by the documentation and I am stuck.
Thanks
Ilir
Support Staff 5 Posted by Paul Batum on 11 Jul, 2010 07:44 AM
I think theres a bug here and that the best thing to do at this stage is a
workaround using a convention:
public class PersonInheritanceConvention : IJoinedSubclassConvention,
IJoinedSubclassConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IJoinedSubclassInspector>
criteria)
{
criteria.Expect(x => x.EntityType.IsSubclassOf(typeof(Person)));
}
public void Apply(IJoinedSubclassInstance instance)
{
instance.Key.Column("personId");
}
}
Essentially what we're saying in this convention is that for all the joined
subclasses of person, their key column is "personId".
I recognise this is not an ideal solution - sorry I can't give you one right
now.