Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate relationships with a shared composite key
    primarykey
    data
    text
    <p>I have two tables with composite keys and a linking table between them, like so:</p> <ul> <li><p>A</p> <ul> <li>aid (PK)</li> <li>sharedkey (PK)</li> </ul></li> <li><p>B</p> <ul> <li>bid (PK)</li> <li>sharedkey (PK)</li> </ul></li> <li><p>Linking</p> <ul> <li>aid</li> <li>bid</li> <li>sharedkey</li> <li>sortorder</li> </ul></li> </ul> <p>So the awkward thing there is the shared key, which is part of both FKs. Here's the first thing I tried (I'm using Fluent, but I would have this same problem with XML mappings):</p> <pre><code>mapping.HasManyToMany(x =&gt; x.Bees) .Table("Linking") .ParentKeyColumns.Add("aid", "sharedkey") .ChildKeyColumns.Add("bid", "sharedkey") .OrderBy("sortorder"); </code></pre> <p>Which gives me an mapping exception: "Repeated column in mapping for collection", because I've put in <code>sharedkey</code> twice. That makes sense, because I haven't told in NH that I actually want <code>sharedkey</code> to be the same in each direction.</p> <p>The Java version of Hibernate has a <code>formula</code> <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/example-mappings.html#example-mappings-composite-key-manytomany" rel="nofollow">feature</a> that solves this exactly, but NH, AFAIK, does not. So how can I map this?</p> <p>Some caveats:</p> <ol> <li>It's not totally necessary for this to be in Fluent. XML is fine too</li> <li>This is all read-only, so if the solution breaks inserts in some funny way, that's fine</li> <li>I can't change the DB at all</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload