1

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

Ram
  • 23
  • 1
  • 6
  • What is your question exactly ? (aside from the fact that update="false" will prevent updates, and that changing the discriminator value dynamically might introduce type inconsistencies in your code) – jbl Apr 24 '13 at 07:21
  • my question is that EmployeeType column should be used for update,insert and select...if i use update="false" insert="false" then it is working fine for Employeetype in the select statement but not for the update or insert . but when i use update="true" or insert="true"...it is not working for even select statement...I need to have EmployeeType discriminator column for select/insert/update – Ram Apr 24 '13 at 07:48
  • the mapping you posted seems fine. I guess you miss the discriminator-value attributes on your class and subclasses mappings The discriminator-value value will be automatically set on inserts, and updates (which would introduce inconsistencies) won't be allowed. – jbl Apr 24 '13 at 08:06

1 Answers1

2

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:

  1. Create a new instance of the class you want and copy the values from the other class instance.

  2. 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.

  3. 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

Community
  • 1
  • 1
mickfold
  • 2,003
  • 1
  • 14
  • 20