title: Hibernate Reverse Engineering Composite Key Creates Extra ID Class
body: <p>I am new to Hibernate and I seem to be having trouble getting Hibernate to correctly reverse engineer a MySQL database (eg: <a href="https://launchpad.net/test-db/" rel="nofollow">https://launchpad.net/test-db/</a>) into Java classes.</p>
  
  <p>In the employees database schema there are several tables:</p>
  
  <ul>
  <li>employees (primary key emp_no)</li>
  <li>department (primary key dept_no)</li>
  <li>salaries (composite keys emp_no and from_date)</li>
  <li>dept_emp (composite keys dept_no and emp_no)</li>
  </ul>
  
  <p>The problem is that when I reverse engineer the table (config and reveng files below), instead of creating the above 4 java classes, Hibernate is giving me 6 Java classes with extra ones <strong>bolded</strong>: </p>
  
  <ul>
  <li>Employees</li>
  <li>Departments</li>
  <li>Salaries</li>
  <li><strong>SalariesId</strong></li>
  <li>DeptEmp</li>
  <li><strong>DeptEmpId</strong></li>
  </ul>
  
  <p>As far as I can tell, for the tables with composite primary keys, Hibernate is creating a new <strong>"classnameID"</strong> class that contains the unique combination of the two composite keys. Instead, it would be awesome to just have, for example, the DeptEmp class with a dept_no and a set/list of emp_no's that are associated with that particular department.</p>
  
  <p>Any ideas how I can get Hibernate not to do this? I've been trying lots of things in the .reveng file, and nothing seems to be working.</p>
  
  <p>Reverse Engineering File:
      
      </p>
  
  <pre><code>&lt;hibernate-reverse-engineering&gt;
     &lt;table-filter match-catalog="employees" match-name="departments" /&gt;
     &lt;table-filter match-catalog="employees" match-name="dept_emp" /&gt;
     &lt;table-filter match-catalog="employees" match-name="employees" /&gt;
     &lt;table-filter match-catalog="employees" match-name="salaries" /&gt;
  
     &lt;table schema="employees" name="departments"&gt;
        &lt;primary-key&gt;
           &lt;key-column name="dept_no" /&gt;
        &lt;/primary-key&gt;
     &lt;/table&gt;   
     &lt;table schema="employees" name="employees"&gt;
        &lt;primary-key&gt;
           &lt;key-column name="emp_no" /&gt;
        &lt;/primary-key&gt;
     &lt;/table&gt;
     &lt;table schema="employees" name="salaries"&gt;
        &lt;primary-key&gt;
           &lt;key-column name="emp_no" /&gt;
           &lt;key-column name="from_date" /&gt;
        &lt;/primary-key&gt;
     &lt;/table&gt;
     &lt;table schema="employees" name="dept_emp"&gt;
        &lt;primary-key&gt;
           &lt;key-column name="emp_no" /&gt;
           &lt;key-column name="dept_no" /&gt;
        &lt;/primary-key&gt;
     &lt;/table&gt;
  &lt;/hibernate-reverse-engineering&gt;
  </code></pre>
  
  <p>Configuration File:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
        &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
        &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/employees&lt;/property&gt;
        &lt;property name="hibernate.connection.username"&gt;****&lt;/property&gt;
        &lt;property name="hibernate.connection.password"&gt;****&lt;/property&gt;
        &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
        &lt;property name="connection.pool_size"&gt;20&lt;/property&gt;
        &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
        &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
        &lt;property name="show_sql"&gt;true&lt;/property&gt;
        &lt;property name="format_sql"&gt;true&lt;/property&gt;
        &lt;property name="use_sql_comments"&gt;true&lt;/property&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Thanks!</p>