title: hibernate one to many using a join table, and hibernate annotations
body: <p>I want do a one-to-many relationship between two tables using a join table.</p>
  
  <p>This is why I want to use a join table:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1307203/hibernate-unidirectional-one-to-many-association-why-is-a-join-table-better">Hibernate unidirectional one to many association - why is a join table better?</a></li>
  <li><a href="https://stackoverflow.com/questions/2092611/why-is-it-recommended-to-avoid-unidirectional-one-to-many-association-on-a-forei">Why is it recommended to avoid unidirectional one-to-many association on a foreign key?</a></li>
  </ul>
  
  <p>Finally, I want to use Hibernate annotations to perform this.</p>
  
  <p>I found some examples to do this using xml mapping but nothing with annotations.</p>
  
  <p>I believe this would be how the tables need to be created</p>
  
  <pre><code>CREATE TABLE `PRODUCT` (
    `PRODUCT_ID`      BIGINT       PRIMARY KEY AUTO_INCREMENT
  );
  
  CREATE TABLE `PARTS` (
    `PART_ID`         BIGINT       PRIMARY KEY AUTO_INCREMENT
  );
  
  -- USER_IMAGE_ID must be unique if we want a one-to-many relationship between PRODUCTS &amp; PARTS tables
  CREATE TABLE `USER_DETAILS_IMAGE` (
    `PRODUCT_ID`      BIGINT,
    `PART_ID`         BIGINT UNIQUE,
    CONSTRAINT `FK_PRODUCT_ID`  FOREIGN KEY (`PRODUCT_ID`)   REFERENCES `PRODUCT`(`PRODUCT_ID`),
    CONSTRAINT `FK_PART_ID`     FOREIGN KEY (`PART_ID`)      REFERENCES `PARTS`(`PART_ID`)
  );
  </code></pre>