views:

15

answers:

1

Okay, I think I have a scenario I haven't seen elsewhere. I have a situation where I have an object type that needs to be able to be changed over time while retaining the history of the changes within the same table (as opposed to just in an audit table).

public class Item 
{
    public virtual int Id { get; set; }
    public virtual ....
    public virtual Item Replaces { get; set; }
    public virtual Item ReplacedBy { get; set; }
}

I am stuck as to how to represent this in Fluent NHIbernate. In the database, I don't need both a Replaces and ReplacedBy field - I can get the info from just one of them. But I think I need both in my object for NHibernate to figure it out.

Currently, I have this, but it generates no database mappings at all:

mapping.HasOne(t => t.ReplacedBy).ForeignKey("Id").Class(typeof(Item));

As always, any thoughts greatly appreciated!

A: 

A HasOne or one-to-one relationship is a bit of a special relationship in NHibernate. It's (typically) an inferred relationship between two separate tables, whereby records that share a primary key value are associated. It's unlikely you'd be able to get a HasOne working for your situation. I have a post on my blog, I think you mean a many-to-one, sir, which goes into some of the misconceptions of HasOne relationships.

Instead you should look at using References, which is a many-to-one relationship, and does support self referential relationships.

James Gregory

related questions