title: Hibernate @ElementCollection - Better solution needed
body: <p>I'm using Hibernate 3.5.a-Final as ORM-Layer in a web application. I've got several Beans with the same code-sniplet wich makes me think that this design isn't the best one around. But I can't figure out how to implement a better one in hibernate.</p>
  
  <p>Requirements</p>
  
  <ul>
  <li>Several classes need to contain localized descriptions in multiple locales</li>
  <li>These need to be persisted into the db</li>
  <li>They have to be searchable by substring for all locales (show up if the seachstring is a substring of any description)</li>
  <li>Localized descriptions should be queryable without loading the master-object (by master-object-id, -type and locale)</li>
  </ul>
  
  <p>Current solution (doesn't solve the last requirement)</p>
  
  <p>Each class contains a HashMap annotated as</p>
  
  <pre><code>@ElementCollection(fetch=FetchType.EAGER)
  @CollectionTable(name = "localized[X]Descriptions", joinColumns = @JoinColumn(name = "id"))
  @MapKeyJoinColumn(name = "locale")
  public Map&lt;Locale, String&gt; getLocalizedDescriptions() {
      return localizedDescriptions;
  }
  </code></pre>
  
  <p>[X] beeing the name of the class</p>
  
  <p>For Each class the is an additional table (generated by hibernate)</p>
  
  <pre><code>create table localized[X]Descriptions (
      id integer not null,
      localizedDescriptions varchar(255),
      localizedDescriptions_KEY varchar(255),
      primary key (id, localizedDescriptions_KEY)
  )
  </code></pre>
  
  <p>For some reason the <code>@MapKeyJoinColumn</code> gets ignored...</p>
  
  <p>What I'd prefer would be a single table like this:</p>
  
  <pre><code>create table localizedDescriptions (
      class varchar(255) not null,
      id integer not null,
      locale varchar(50) not null,
      description varchar(255) not null,
      primary key (class, id, locale)
  )
  </code></pre>
  
  <p>It would be a big plus if the implementation would be queryable using the criteria-api (which isn't compatible to <code>@ElementCollection</code>s as far as I know).
  But I can't figure out how to implement this. Any pointers would be very welcome</p>