title: How to map combinations of things to a relational database?
body: <p>I have a table whose records represent certain objects. For the sake of simplicity I am going to assume that the table only has one column, and that is the unique <code>ObjectId</code>. Now I need a way to store combinations of objects from that table. The combinations have to be unique, but can be of arbitrary length. For example, if I have the <code>ObjectId</code>s</p>
  
  <pre><code>1,2,3,4
  </code></pre>
  
  <p>I want to store the following combinations:</p>
  
  <pre><code>{1,2}, {1,3,4}, {2,4}, {1,2,3,4}
  </code></pre>
  
  <p>The ordering is not necessary. My current implementation is to have a table <code>Combinations</code> that maps <code>ObjectId</code>s to <code>CombinationId</code>s. So every combination receives a unique Id:</p>
  
  <pre><code>ObjectId | CombinationId
  ------------------------
  1        | 1
  2        | 1
  1        | 2
  3        | 2
  4        | 2
  </code></pre>
  
  <p>This is the mapping for the first two combinations of the example above. The problem is, that the query for finding the <code>CombinationId</code> of a specific Combination seems to be very complex. The two main usage scenarios for this table will be to iterate over all combinations, and the retrieve a specific combination. The table will be created once and never be updated. I am using <a href="http://www.sqlite.org/" rel="nofollow noreferrer">SQLite</a> through JDBC. Is there any simpler way or a best practice to implement such a mapping?</p>