title: Hibernate mapping for situation, where the the key is also used as many-to-one parameter
body: <p>I was wondering if there is a way to do Hibernate mapping for the following. My two days spent on this say there isn't.</p>
  
  <p>There are two tables, having <code>one to many</code> relationship.</p>
  
  <pre><code>+---------------------+       +-----------------+
  |  versioned_entity   |       |  basic_entity   |
  +---------------------+       +-----------------+
  | fk_BasicEntityId    | *   1 | id              |
  | version             |-------| active_version  |
  | ...                 |       | ...             |
  +---------------------+       +-----------------+
  </code></pre>
  
  <p>Hibernate mapping:</p>
  
  <pre><code>&lt;class name="my.VersionedEntity" table="versioned_entity"&gt;
    &lt;composite-id&gt;
      &lt;key-many-to-one name="basicEntity" column="fk_BasicEntityId"/&gt;
      &lt;key-property name="version"/&gt;
    &lt;/composite-id&gt;
    ...
  &lt;/class&gt;
  
  &lt;class name="my.BasicEntity" table="basic_entity"&gt;
    &lt;id name="id"&gt;
    &lt;property name="activeVersion" not-null="true"&gt;
      &lt;column name="active_version"/&gt;
    &lt;/property&gt;
    ...
  &lt;/class&gt;
  </code></pre>
  
  <p>Brief explanation: <code>fk_BasicEntityId</code> is a foreign key pointing to <code>basic_entity.id</code>. An entry in <code>versioned_entity</code> can be identified by its foreign key and <code>version</code> (there can be many versions having same <code>fk_BasicEntityId</code>). <code>active_version</code> is a number pointing to one of <code>versioned_entity</code> entries and tells which version is "active". Hope that's clear enough.</p>
  
  <p>Now the problem I am solving is that I need quick access in Java like this:</p>
  
  <pre><code>my.BasicEntity basic = my.BasicEntityDAO.get();
  my.VersionedEntity activeVersion = basic.getActiveVersionedEntity();
  </code></pre>
  
  <p>The current implementation is done in Java and simply iterates over all versioned entities of given basic entity and checks if its version matches with active version. Needless to say, this doesn't scale and results performance problems.</p>
  
  <p>So I went on to move this onto database level. Looks easy? So I thought! I went on and added following into Hibernate mapping of basic_entity:</p>
  
  <pre><code>&lt;many-to-one name="activeVersionedEntity" not-null="false" insert="false" update="false"&gt;
    &lt;column name="id" unique="true" not-null="true"/&gt;
    &lt;column name="activeVersion"/&gt;
  &lt;/many-to-one&gt;
  </code></pre>
  
  <p>The double column is required here for <code>versioned_entity</code> has composite key.
  Unfortunately, this doesn't work - JVM dies without a trace when a call to <code>be.getActiveVersionedEntity()</code> is invoked (on colleague's machine it throws StackOverflowError inside Hibernate). I suspect this happens because "id" is also an identifier, as I have very similar mapping for the same class where column is not an identifier and this works correctly.</p>
  
  <p>I could probably do this with HQL, but that would require me to move this method into the DAO class which is particularly awkward as would require changes in API (not an option). Using DAO call inside <code>my.BasicEntity::getActiveVersionedEntity()</code> is another big no-no: I can't mix up different abstraction layers. Another way of solving this would be looking into Interceptors/events, however this also probably one of those options which beat the purpose of the framework (I shouldn't care about low level things like this, do I?).</p>
  
  <p>I am running Hibernate version: 3.2.7GA / Spring 2.5.6. The documentation is unbelievably poor - I have looked at three different Hibernate books, online documentation, forums, etc.</p>
  
  <p>Any help is appreciated.</p>