Skip to content Skip to sidebar Skip to footer

Nhibernate Composedid On Intermediate Table

I want to have a intermediate table with only two foreign keys (as a ComposedId). But NHibernate is automatically creating a 'id' property. I have the following classes public clas

Solution 1:

Let me give you suggestion, just my point of view - do not use composite id. Use standard primary key in DB and its C# / entity representation as Id { get; set; }

Chapter 24. Best Practices

...

Declare identifier properties on persistent classes.

NHibernate makes identifier properties optional. There are all sorts of reasons why you should use them. We recommend that identifiers be 'synthetic' (generated, with no business meaning) and of a non-primitive type. For maximum flexibility, use Int64 or String.

See also more about synthetic, surrogate keys at wiki.

From my experience, we should not be worry about having pairing object like this:

publicclassLaceHasCard
{
    publicvirtualint Id { get; set; } // the keypublicvirtual Card Card { get; set; }
    publicvirtual Lace Lace { get; set; }
}

Because later it would become so easy to access it:

session.Get<LaceHasCard>(id)

And also to use it in Subqueries(for filtering Card with Laces and vice versa)

One column in DB, autogenerated, should not have any extra bad impact. But handling such table is a bit (a lot) easier...

So, summary, my suggestion would be, make all entities first level citizens, with full rights (including synthetic/surrogate key)

Post a Comment for "Nhibernate Composedid On Intermediate Table"