Problem Description
=============
I am following the Table per subclass mapping strategy as described in "NHibernate in Action" (Draft) book by the NHibernate
authors.
The difference in my case is that the subclass tables have their own primary key! So any association between the subclass
tables with other child tables is based on the primary key of the subclass table. But NHibernate generates insert sql
statements for the child tables of the joined-subclass assuming that the PK of the subclass table is same as the PK of the
parent table.
Mapping (partial listing)
Code:
<class name="Quote" table="quote">
<id name="Id" column="quote_id" unsaved-value="0">
<generator class="identity"/>
</id>
<property column="name" type="String" name="Name" />
<property column="eff_date" type="DateTime" name="EffectiveDate" />
<joined-subclass name="MultiLineQuote" table="multiline_quote">
<key column="quote_id"/>
<one-to-one name="MultilineCustomer" class="MultilineCustomer"
property-ref="MultilineQuote" cascade="save-update"/>
<property column="commssn" type="Single" name="Commssn" />
<property column="prop_irpm" type="Single" name="PropIrpm" />
</joined-subclass>
</class>
<class name="MultilineCustomer" table="multiline_customer">
<id name="Id" column="multiline_cust_id" unsaved-value="0">
<generator class="identity"/>
</id>
<bag name="BusinessOperationStateList" inverse="true" lazy="true" >
<key column="multiline_cust_id" />
<one-to-many class="BusinessOperationState" />
</bag>
<many-to-one name="MultilineQuote" class="Quote" column="multiline_quote_id" unique="true"/>
<property column="commercl_auto_cov_quote_flag" type="Int32" name="CommerclAutoCovQuoteFlag" />
</class>
Tables (partial listing)
CREATE TABLE [quote] (
[quote_id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[eff_date] [datetime] NULL ,
CREATE TABLE [multiline_quote] (
[multiline_quote_id] [int] IDENTITY (1, 1) NOT NULL ,
[quote_id] [int] NOT NULL ,
[commssn] [real] NULL ,
CREATE TABLE [multiline_customer] (
[multiline_cust_id] [int] IDENTITY (1, 1) NOT NULL ,
[multiline_quote_id] [int] NOT NULL ,
[commercl_auto_cov_quote_flag] [int] NULL ,
I know the table schema is not ideal. But this is an existing legacy database, which we have to use. Logically the three
tables should be mapped to one class. For that I need to create a nested <joined-subclass elements in the mapping. I tried
that and faced the same problem as described below. So,I tried this alternate approach of <one-to-one> property association.
Prototype Code sample
=====================
AddQuote.aspx.vb (partial listing)
==================================
.....
.....
Code:
Dim quoteDao As IQuoteDao = DaoFactory.GetQuoteDao
Dim myQuote As MultiLineQuote = quoteDao.GetById(GetQuoteId, False)
myQuote.SetMultilineCustomer(New MultilineCustomer)
MultliLineQuote.vb (partial listing)
====================================
......
.......
Code:
Public Overridable Sub SetMultilineCustomer(ByVal myMultilineCustomer As MultilineCustomer)
If myMultilineCustomer Is Nothing Then Throw New ArgumentNullException
Me.MultilineCustomer = myMultilineCustomer
myMultilineCustomer.MultilineQuote = Me
End Sub
When I associate the MultiLineQuote object with the MultilineCustomer, a sql statement for insert is generated. But it tries
to bind the "quote_id" value from the "quote" table to the "multiline_quote_id" column in the MultilineCustomer table.
Instead it should bind the multiline_quote_id value from the multiline_quote table to the multiline_quote_id column in the
multiline_customer table.
Question
========
Is there a way of telling NHibernate that the joined-subclass table has its own primary key?
Thanks
Arvind