NHibernate - Subtype Change

How are you going to change the subtype of a string in NHibernate? For example, if I have a Customer object and a subclass of TierOneCustomer, I have a case where I need to change the client to TierOneCustomer, but TierOneCustomer must have the same identifier (PK) as the original Customer object.

The display looks something like this:

<class name="Customer" table="SiteCustomer" discriminator-value="C">
  <id name="Id" column="Id" type="Int64">
    <generator class="identity" />
  </id>
  <discriminator column="CustomerType" />
  ... properties snipped ...

  <subclass name="TierOneCustomer" discriminator-value="P">
    ... more properties ...
  </subclass>
</class>

I use one table for each class hierarchy, so using simple sql, it will just be a matter of sql-update discriminator (CustomerType) and set the appropriate columns corresponding to the type. I cannot find a solution in NHibernate, so appreciate any pointers.

, , , , , , . , .

+5
4

- , (), SQL.

, , NHibernate , , , "" Java, .

. , , , ( ). :

  • TierOneCustomer Customer, . , .

  • , () . , , TierOneCustomer , , , Customer.Tier = 1.

Hibernate, :

+9

- .

, , . .NET Java. . , ( ). , , , , , - , - , . , - , .

, . , ! . , , , . :

  • , , .

, , . . , IsTierOne DiscountStrategy .., , .

NHibernate ( Hibernate Java) , . , , . NHibernate , , NHibernate .

+5

, , - :

, , NH ( SQL), . FluentNH:

public enum CustomerType //not sure it needed
{
   Customer,
   TierOneCustomer
}

public class Customer
{
   //You should be able to use the Type name instead,
   //but I know this enum-based approach works
   public virtual CustomerType Type 
   { 
      get {return CustomerType.Customer;} 
      set {} //small code smell; setter exists, no error, but it doesn't do anything.
   }
   ...
}

public class TierOneCustomer:Customer
{
   public override CustomerType Type {get {return CustomerType.TierOneCustomer;} set{}}
   ...
}

public class CustomerMap:ClassMap<Customer>
{
   public CustomerMap()
   {
      ...
      DiscriminateSubClassesOnColumn<string>("CustomerType");
      DiscriminatorValue(CustomerType.Customer.ToString());
      //here the magic; make the discriminator updatable
      //"Not.Insert()" is required to prevent the discriminator column 
      //showing up twice in an insert statement
      Map(x => x.Type).Column("CustomerType").Update().Not.Insert();
   }
}

public class TierOneCustomerMap:SubclassMap<TierOneCustomer>
{
   public CustomerMap()
   {
      //same idea, different discriminator value
      ...
      DiscriminatorValue(CustomerType.TierOneCustomer.ToString());
      ...
   }
}

, - , , ( un - ), , . , AFAIK NHibernate , (, , " " ); NHibernate, - , ?

, "", , "" ( "" ). " " / .., , , OP. OP TierOneCustomer -, , .

+2

(, script), SQL , .

, , , , , .

(, URL-), , , -. , (, , ).

+1

All Articles