title: Hibernate 4: Partial many-to-one association + where/filter discriminator
body: <p>I'm using Hibernate 4. I have this database with Category and CategoryItem tables:</p>
  
  <pre><code>CREATE  TABLE `test`.`Category` (
    `Id` INT NOT NULL AUTO_INCREMENT ,
    `Description` VARCHAR(50) NOT NULL ,
    PRIMARY KEY (`Id`) )
  
  CREATE  TABLE `test`.`CategoryItem` (
    `IdCategory` INT NOT NULL ,
    `Id` VARCHAR(10) NOT NULL ,
    `Description` VARCHAR(100) NOT NULL ,
    PRIMARY KEY (`Id`, `IdCategory`) ,
    CONSTRAINT `fk_table1_Category1`
      FOREIGN KEY (`IdCategory` )
      REFERENCES `test`.`Category` (`Id` ))
  </code></pre>
  
  <p>This are used to store different categories or catalogs and its values. For example:</p>
  
  <p>Category table:</p>
  
  <pre><code>1, 'Genders'
  2, 'Marital status'
  3, 'Countries'
  </code></pre>
  
  <p>CategoryItem table:</p>
  
  <pre><code>1, 'FEM', 'Female'
  1, 'MAL', 'Male'
  2, 'SIN', 'Single'
  2, 'MAR', 'Married'
  3, 'US', 'United States'
  3, 'UK', 'England'
  </code></pre>
  
  <p>These values are used all over the schema, but with logical reference to CategoryItem(Id) only, for example:</p>
  
  <pre><code>CREATE  TABLE `test`.`Person` (
    `Id` INT NOT NULL ,
    `Name` VARCHAR(30) NULL ,
    --this fields are referencing CategoryItem(Id)
    `IdGender` VARCHAR(10) NOT NULL ,
    `IdMaritalStatus` VARCHAR(10) NOT NULL ,
    `IdCountryOrigen` VARCHAR(10) NOT NULL ,
    PRIMARY KEY (`Id`) )
  </code></pre>
  
  <p>Is it possible to map this tables with some technique in Hibernate? I'm not able to change this schema right now.</p>
  
  <p><strong>EDITED:</strong></p>
  
  <p>These are the classes and mappings so far:</p>
  
  <pre><code>public class Category implements Serializable {
  
      private int id;
      private String description;
  
      private Set&lt;CategoryItem&gt; items = new HashSet&lt;CategoryItem&gt;();
  
      public Category() {
      }
  
      //getters and setters omitted
  }
  
  public class CategoryItem implements Serializable {
  
      private CategoryItemId id;
      private Category category;
      private String description;
  
      public CategoryItem() {
      }
      //getters and setters omitted
  }
  
  public class CategoryItemId implements Serializable {
  
      private Category category;
      private String id;
  
      public CategoryItemId() {
      }
      //getters and setters omitted
  }
  
  public class Person implements Serializable {
  
      private int id;
      private String name;
      private CategoryItem gender;
      private CategoryItem maritalStatus;
      private CategoryItem countryOrigen;
  
      public PersonaNatural() {
      }
      //getters and setters omitted
  }
  
    &lt;class catalog="test" name="test.Category" table="Category"&gt;
      &lt;id name="id" type="int"&gt;
        &lt;column name="Id"/&gt;
        &lt;generator class="increment"/&gt;
      &lt;/id&gt;
      &lt;property name="description" type="string"&gt;
        &lt;column length="50" name="Description" not-null="true"/&gt;
      &lt;/property&gt;
      &lt;set inverse="true" name="items"&gt;
        &lt;key&gt;
          &lt;column name="IdCategory" not-null="true"/&gt;
        &lt;/key&gt;
        &lt;one-to-many class="test.CategoryItem"/&gt;
      &lt;/set&gt;    
    &lt;/class&gt;
  
    &lt;class catalog="test" name="test.CategoryItem" table="CategoryItem"&gt;
      &lt;composite-id class="test.CategoryItemId" name="id"&gt;
          &lt;key-many-to-one name="category" class="test.Category"&gt;
              &lt;column name="IdCategory" not-null="true"/&gt;
          &lt;/key-many-to-one&gt;
        &lt;key-property name="id" type="string"&gt;
          &lt;column name="Id"/&gt;
        &lt;/key-property&gt;
      &lt;/composite-id&gt;
      &lt;many-to-one name="category" class="test.Category" fetch="select" insert="false" update="false"&gt;
        &lt;column name="IdCategory" not-null="true"/&gt;
      &lt;/many-to-one&gt;
      &lt;property name="description" type="string"&gt;
        &lt;column name="Description" length="100" not-null="true"/&gt;
      &lt;/property&gt;
    &lt;/class&gt;
  
  &lt;class catalog="test" name="test.Person" table="Person"&gt;
      &lt;id name="id" type="int"&gt;
        &lt;column name="Id"/&gt;
        &lt;generator class="increment"/&gt;
      &lt;/id&gt;
      &lt;property name="name" type="string"&gt;
        &lt;column length="30" name="Name" not-null="false"/&gt;
      &lt;/property&gt;
      &lt;many-to-one name="gender" class="test.CategoryItem" fetch="select"&gt;
          &lt;column name="IdGender" not-null="true"/&gt;
      &lt;/many-to-one&gt;
      &lt;many-to-one name="maritalStatus" class="test.CategoryItem" fetch="select"&gt;
          &lt;column name="IdMaritalStatus" not-null="true"/&gt;
      &lt;/many-to-one&gt;
      &lt;many-to-one name="countryOrigen" class="test.CategoryItem" fetch="select"&gt;
          &lt;column name="IdCountryOrigen" not-null="true"/&gt;
      &lt;/many-to-one&gt;
    &lt;/class&gt;
  </code></pre>
  
  <p>I'm trying with many-to-one but I'm getting this error: <strong>'must have same number of columns as the referenced primary key'</strong>. This makes sense because I'm pretending to map just CategoryId(Id) on Person.hbm.xml.</p>
  
  <p>I'm gonna need to specific a <em>where</em> or <em>discriminator</em> value to complete the left join condition, because CategoryItem(Id) alone is not unique.</p>
  
  <p>Maybe this is not even possible with Hibernate, but any help I will appreciate.
  Thanks.</p>