NHibernate mapping with two special cases

Posted by brainimus on Stack Overflow See other posts from Stack Overflow or by brainimus
Published on 2011-03-04T14:37:21Z Indexed on 2011/03/04 15:24 UTC
Read the original article Hit count: 243

I am using NHibernate to map a class to a database table. The Part table has an ID column (primary key) and a ParentPart column (along with a few others).

class Part
{
    public virtual long ID{ get; set; }
    public virtual Part ParentPart { get; set; }   
}

The ParentPart is normally another valid part in the part table but I have two special cases. I have a case where the ParentPart column can be 0 (zero) and another case where it can be -1. Neither of these cases currently represent another valid Part object. I was thinking I could make 2 subclasses of Part (ZeroPart and NegativeOnePart) that would never persist. I want the zero and -1 values to be entered in the column but not persist the entire ZeroPart or NegativeOnePart objects. I am unsure how to map this (I'm using hbm files) or if this even the correct approach.

How can I map this so that normal valid parts are persisted but I can also handle the special cases?

As an aside: My current hbm file has the Part.ID's unsaved value as zero but I think I can just change this in the mapping to something different and default it in the class.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping