title: Composite Key with Hibernate
body: <p>In order to generate the next SQL code: </p>
  
  <pre><code>create table users (
      user_name varchar(15) not null primary key, 
      user_pass varchar(15) not null);
  
  create table user_roles(
      username varchar(15) not null,
      role_name varchar(15) not null, 
      primary key(usernmae, rolename)
  );
  </code></pre>
  
  <p>You can use code such as: </p>
  
  <pre><code> &lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="databaselayer.users.UserDB" table="users"&gt;
          &lt;id name="username" type="string" column="user_name"&gt;
                  &lt;meta attribute="scope-set"&gt;public&lt;/meta&gt;      
          &lt;/id&gt;    
          &lt;property name="password" type="string" column="user_pass"  not-null="true"/&gt;
          &lt;set name="roles" cascade="save-update" inverse="true"&gt;
                  &lt;key column="user_name"/&gt;
                  &lt;one-to-many class="databaselayer.users.RoleDB"/&gt;
          &lt;/set&gt;                       
      &lt;/class&gt;
     &lt;class name="databaselayer.users.RoleDB" table="user_roles"&gt;
          &lt;composite-id&gt;
              &lt;key-many-to-one name="username" class="databaselayer.users.UserDB" column="user_name"/&gt;
              &lt;key-property name="role" type="string" column="role_name"/&gt;
          &lt;/composite-id&gt;             
      &lt;/class&gt;          
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>You may need to have your classes as well as hbm mapping files in your classpath.<br>
  Next I post my Ant schema target:<br>
  <strong>Note</strong>  ${basedir}/build/WEB-INF/classes contains both *.class and *.hbm.xml files.</p>
  
  <pre><code>&lt;target name="schema" description="Generate DB schema from the O/R mapping files"&gt;
      &lt;hibernatetool destdir="${basedir}"&gt;            
          &lt;classpath path="${basedir}/build/WEB-INF/classes"&gt;
              &lt;fileset dir="${basedir}/build/WEB-INF/classes"&gt;
              &lt;include name="**/*"/&gt;
          &lt;/fileset&gt;              
          &lt;/classpath&gt;
          &lt;configuration configurationfile="${basedir}/hibernate.cfg.xml"/&gt;
          &lt;hbm2ddl 
              drop="true" 
              create="true"
              export="true"
              outputfilename="libamo2-ddl.sql"
              delimiter=";"
              format="true"/&gt;       
      &lt;/hibernatetool&gt;
  &lt;/target&gt;
  </code></pre>
  
  <h2>Related links from Hibernate documentation</h2>
  
  <p><a href="http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-compositeid" rel="noreferrer">Composite ID</a><br>
  <a href="http://www.hibernate.org/hib_docs/reference/en/html/components-compositeid.html" rel="noreferrer">Components as composite Id</a></p>