title: How to map many value type collections to one single table in Hibernate?
body: <p>I am tring to map many value-type sets in hibernate to one table for performance reasons. 
  First I did put them all in their own table which led to too many joins.</p>
  
  <p>I have a Categories class that contains many components with a bunch of sets and then I map them to a single table through subclasses by using the entity-name and discriminator.
  See below for the mapping snippet.</p>
  
  <p>This works fine, but because the single table that contains all the sets, it's hard to automatically remove a set when a component is removed. This is because it's modelled as an Entity and Hibernate can't signal that the set has been removed.</p>
  
  <p>Mapping details:</p>
  
  <pre><code>&lt;class name="com.sample.CategoriesDefault" table="dec_cats" &gt;
   &lt;id name="id" column="id" type="string" length="40" access="property"&gt;
    &lt;generator class="assigned" /&gt;
   &lt;/id&gt;
   &lt;component name="incomeInfoMember" class="com.sample.IncomeInfoDefault"&gt;
     &lt;property name="hasWage" type="boolean" column="inMemWage"/&gt;
      ...
     &lt;component name="wage" class="com.sample.impl.WageDefault"&gt;
       &lt;property name="hasEmployerWage" type="boolean" column="inMemEmpWage"/&gt;
        ...
       &lt;set name="employerWages" cascade="all-delete-orphan" lazy="false"&gt;
        &lt;key column="idCats" not-null="true" /&gt;
        &lt;one-to-many entity-name="mIWaEmp"/&gt;
       &lt;/set&gt;
     &lt;/component&gt;
   &lt;/component&gt;
  &lt;/class&gt;
  
  &lt;class name="com.sample.NameValueTypeEntityMarker" table="cats_name_amount"&gt;
   &lt;id name="id" column="id" type="string" length="40" access="property"&gt;
    &lt;generator class="assigned" /&gt;
   &lt;/id&gt;
   &lt;discriminator type="string" force="true"&gt;
     &lt;column name="objType" index="ixDiscrCatsNameAmt" length="25" not-null="true" /&gt;
   &lt;/discriminator&gt;
   &lt;version name="version" column="objVrs"  unsaved-value="negative"/&gt;
  
   &lt;property name="name" type="string" column="name" not-null="true" /&gt;
   &lt;subclass name="com.sample.NameAmountsIntValueTypeEntityMarker"&gt;
    &lt;property name="amount1" type="integer" column="amount1" access="property" not-null="true"/&gt;
     ...
     &lt;subclass entity-name="mIWaEmp" name="com.sample.EmployerWageDefault" discriminator-value="mIWaEmp" /&gt;
     &lt;subclass entity-name="pIWaEmp" name="com.sample.impl.EmployerWageDefault" discriminator-value="pIWaEmp" /&gt;
   &lt;/subclass&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>Categories cats = new CategoriesDefault();
  Wage wage = new Wage();
  Set&lt;EmployerWage&gt; set = new HashSet&lt;EmployerWage&gt;();
  set.add(new EmployerWageDefault(cats));
  wage.setEmployerWages(set);
  IncomeMember inc = new IncomeMemberDefault();
  inc.setWage(wage);
  cats.setIncomeMember(inc);
  cats.saveOrUpdate(); // will store it in the db.
  
  // now remove the income:
  cats.setIncomeMember(null);
  cats.saveOrUpdate();
  cats = getPersister().findCats(cats.getId());
  // ERROR: cats still contains IncomeMember, Wage and EmployerWages.
  </code></pre>
  
  <p>This errors occurs as the EmployerWages aren't automatically removed as they are marked as child's of Categories (see the key column) and so they are removed as orphan's when Categories is removed and not when of his components is removed...
  However, I can't make EmployerWages a child of Wage as Wage is a component and Hibernate complaints then...</p>
  
  <p>Pffff... very hard to solve this. Idea's are more then welcome...</p>