Actually I need it because the discriminator column is part of the
table's primary key! If don't create a property or field for my class
and don't specify it in the mapping, I get an NonUniqueObjectException
when trying to add an instance of both of the derived classes with the
same Id.
Example 1
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...>
<class name="Shape" table="shape">
<composite-id>
<key-property name="Kind"> <column name="kind"/> </key-
property>
<key-property name="Id"> <column name="id"/> </key-
property>
</composite-id>
<discriminator column="kind" insert="false" />
<subclass name="Rectangle" discriminator-value="R" />
<subclass name="Circle" discriminator-value="C"/>
</class>
</hibernate-mapping>
class Shape
{
private char kind;
private int id;
public virtual char Kind { get { return kind; } set { kind =
value; } }
public virtual int Id { get { return id; } set { id = value; } }
...
}
class Rectangle : Shape { ... }
class Circle : Shape { ... }
Example 2
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...>
<class name="Shape" table="shape">
<composite-id>
<key-property name="Id"> <column name="id"/> </key-
property>
</composite-id>
<discriminator column="kind"/>
<subclass name="Rectangle" discriminator-value="R" />
<subclass name="Circle" discriminator-value="C"/>
</class>
</hibernate-mapping>
abstract class Shape
{
private int id;
public virtual int Id { get { return id; } set { id = value; } }
...
}
class Rectangle : Shape { ... }
class Circle : Shape { ... }
The first example works fine but the Kind property is quite useless
and really weird. It is clear that the Kind property will be "R" for
rectangles and "C" for circles!
The second example doen't work. When I try the following
var rect = new Rectangle() { Id = 10 };
session.Save(rect);
var circ = new Rectangle() { Id = 10 };
session.Save(circ);
I get the NonUniqueObjectException. In my database, both (rectangles
and circles) are diferent entities that share a common ancestor and
are mapped to the same database table and even though they have the
same Id, the rectangle 10 is not the same as the circle 10 because a
discriminator column is in the primary key.
(Actually, what we have in our legacy database is a table shared for
customers and suppliers.)
Any ideas?
Thanks for your interest!