Fluent NHibernate - subclasses with shared reference
Posted
by ollie
on Stack Overflow
See other posts from Stack Overflow
or by ollie
Published on 2010-02-04T23:01:42Z
Indexed on
2010/03/27
5:13 UTC
Read the original article
Hit count: 447
Edit: changed class names.
I'm using Fluent NHibernate (v 1.0.0.614) automapping on the following set of classes (where Entity is the base class provided in the S#arp Architecture framework):
public class Car : Entity
{
public virtual int ModelYear { get; set; }
public virtual Company Manufacturer { get; set; }
}
public class Sedan : Car
{
public virtual bool WonSedanOfYear { get; set; }
}
public class Company : Entity
{
public virtual IList<Sedan> Sedans { get; set; }
}
This results in the following Configuration (as written to hbm.xml):
<class name="Company" table="Companies">
<id name="Id" type="System.Int32" unsaved-value="0">
<column name="`ID`" />
<generator class="identity" />
</id>
<bag cascade="all" inverse="true" name="Sedans" mutable="true">
<key>
<column name="`CompanyID`" />
</key>
<one-to-many class="Sedan" />
</bag>
</class>
<class name="Car" table="Cars">
<id name="Id" type="System.Int32" unsaved-value="0">
<column name="`ID`" />
<generator class="identity" />
</id>
<property name="ModelYear" type="System.Int32">
<column name="`ModelYear`" />
</property>
<many-to-one cascade="save-update" class="Company" name="Manufacturer">
<column name="`CompanyID`" />
</many-to-one>
<joined-subclass name="Sedan">
<key>
<column name="`CarID`" />
</key>
<property name="WonSedanOfYear" type="System.Boolean">
<column name="`WonSedanOfYear`" />
</property>
</joined-subclass>
</class>
So far so good! But now comes the ugly part. The generated database tables:
Table: Companies
Columns: ID (PK, int, not null)
Table: Cars
Columns: ID (PK, int, not null)
ModelYear (int, null)
CompanyID (FK, int, null)
Table: Sedan
Columns: CarID (PK, FK, int, not null)
WonSedanOfYear (bit, null)
CompanyID (FK, int, null)
Instead of one FK for Company, I get two!
How can I ensure I only get one FK for Company? Override the automapping? Put a convention in place? Or is this a bug? Your thoughts are appreciated.
© Stack Overflow or respective owner