title: NHibernate Bag Mapping
body: <p>I need a hand mapping a collection.</p>
  
  <p>I am using a join table to hold references to rooms ids and calEvent Ids.
  However, the look ups will mainly be based on the room's <code>buildingID</code> - so I'd like to add <code>buildingID</code> to the following mapping. Does anyone know how to do so?</p>
  
  <p>I've looked through the NHibernate docs but can't find anything.</p>
  
  <pre><code>&lt;bag name="rooms" table="tb_calEvent_rooms" lazy="false"&gt;
    &lt;key column="calEventID"/&gt;
    &lt;many-to-many class="BasicRoom" column="roomID"/&gt;
  &lt;/bag&gt;
  </code></pre>
  
  <p>Class:</p>
  
  <pre><code>public class BasicRoom 
  {
      private long _id;
      private long _buildingID;
      private string _roomName;
  </code></pre>
  
  <p>Any hints/help much appreciated.</p>
  
  <p><strong>Reasoning:</strong></p>
  
  <p>A room will never change building so if I add buildingID to the join table;
  I will reduce the complexity of the select statement from something like -</p>
  
  <pre><code>SELECT *
  FROM
    dbo.tb_calEvent_rooms
    INNER JOIN dbo.tb_calEvents ON (dbo.tb_calEvent_rooms.calEventID = dbo.tb_calEvents.id)
    INNER JOIN dbo.tb_rooms ON (dbo.tb_calEvent_rooms.roomID = dbo.tb_rooms.id)
    INNER JOIN dbo.tb_buildings ON (dbo.tb_rooms.buildingID = dbo.tb_buildings.id)
  WHERE
    dbo.tb_buildings.id = 54
  </code></pre>
  
  <p>To</p>
  
  <pre><code>SELECT *
  FROM
    dbo.tb_calEvents
    INNER JOIN dbo.tb_calEvent_rooms ON (dbo.tb_calEvents.id = dbo.tb_calEvent_rooms.calEventID),
    dbo.tb_buildings
  WHERE
    dbo.tb_buildings.id = 54
  </code></pre>
  
  <p>Is my reasoning correct?</p>