Reputation: 23
I have a discriminator column in the which i need to use for select insert and update. I need to update and select the Employee type. I have following code in the mapping xml file.
<discriminator column="EmployeeType" type="String"/>
<property name="EmployeeType" column="EmployeeType" update="false" insert="false" type="String" />
Please assist me
Upvotes: 1
Views: 4343
Reputation: 2003
It is not possible to do what you want to do because changing the discriminator value would change the class type.
The only possible solutions are to:
Create a new instance of the class you want and copy the values from the other class instance.
Don't use inheritance and discriminators to store the type information. Use an enumerated property instead. This will allow you to change the type of the employee on the fly.
Update the discriminator using an SQL Update statement.
An example of why it does not make sense to this is the following. Say you have the following classes:
public abstract class Animal
{
public virtual Guid id { get; set; }
public virtual string AnimalType { get; set; }
...
}
public class Dog : Animal
{
public virtual bool loves_walkies { get; set; }
}
public class Cat : Animal
{
public virtual bool is_agile { get; set; }
}
With the mapping something like
<class name="Earth.Animal">
...
<discriminator column="AnimalType" type="String"/>
<property name="AnimalType" type="String" />
<subclass name="Earth.Cat" discriminator-value="Cat">
<property name="loves_walkies" />
</subclass>
<subclass name="Earth.Dog" discriminator-value="Dog">
<property name="is_agile" />
</subclass>
</class>
Now if NHibernate allowed you to do something like the following:
var dog = session.Get<Dog>(guid);
dog.AnimalType = "Cat";
session.SaveOrUpdate(dog);
What would the results be? Does NHibernate persist the loves_walkies
property because the object is a Dog
, but look the discriminator says it is a cat so it needs to persist the is_agile
property.
To avoid this confusion NHibernate make the discriminator column read-only.
This question has been asked before, please see the links below for further reading:
https://groups.google.com/forum/?fromgroups=#!topic/nhusers/_Qqi4WCu2Bk
NHibernate - Changing sub-types
Upvotes: 2