I have a DB which uses more than 1 table to map an inheritance structure.
The DB (MS SQL) looks like:
Code:
CREATE TABLE [dbo].[A](
[xp] [int] NOT NULL,
[xa] [datetime] NULL,
[s] [varchar](50) NULL,
CREATE TABLE [dbo].[B](
[xp] [int] NOT NULL,
[xa] [datetime] NULL,
[t] [varchar](50) NULL,
sadly, the columns xp and xa appear in both tables.
The mapping is:
Code:
<?xml version='1.0'?>
<hibernate-mapping auto-import="false" xmlns="urn:nhibernate-mapping-2.2">
<class name="nhtest.A, nhtest" table="A">
<id name="xp" column="xp" type="Int32">
<generator class="assigned" />
</id>
<property name="xa" column="xa" type="DateTime" />
<property name="s" column="s" type="String" />
</class>
<joined-subclass extends="nhtest.A, nhtest" name="nhtest.B, nhtest " table="B">
<key column="xp" />
<property name="xp" column="xp" type="Int32" />
<property name="xa" column="xa" type="DateTime" />
<property name="t" column="t" type="String" />
</joined-subclass>
</hibernate-mapping>
and the classes are:
Code:
class A
{
protected Int32? _xp = null;
protected DateTime? _xa = null;
protected string _s = null;
public virtual Int32? xp
{
get {return _xp;}
set {_xp = value;}
}
public virtual DateTime? xa
{
get {return _xa;}
set {_xa = value;}
}
public virtual string s
{
get {return _s;}
set {_s = value;}
}
}
class B : A
{
protected new Int32? _xp = null;
protected new DateTime? _xa = null;
protected string _t = null;
public virtual new Int32? xp
{
get {return _xp;}
set {_xp = value;}
}
public virtual new DateTime? xa
{
get {return _xa;}
set {_xa = value;}
}
public virtual string t
{
get {return _t;}
set {_t = value;}
}
}
When I attempt to insert into an instance of "B":
Code:
B c = new B();
c.xp = 36;
((A)c).xp = 48;
session.Save(c);
session.Flush();
NHibernate generates:
Code:
NHibernate: INSERT INTO B (xp, xa, t, xp) VALUES (@p0, @p1, @p2, @p3); @p0 = '36
', @p1 = '', @p2 = '', @p3 = '36'
NHibernate has grabbed "36" for both instances of the member "xp", when I expected it to grab "36" for B.xp and 48 for A.xp. Additionally the generated SQL does not specifiy table names on the inserts, so it's ambiguous which "xp" column is to be inserted into.
It's unfortunate that I cannot change the DB structure; I must find a way to map it, as it is.
Is there a way to tell NHibernate that there is an xp in both table "A" and table "B" which maps to "xp" in class "A" and class "B"?
Thank-you, in advance, for any help you can offer.