title: ManyToMany Mapping (Helper Table) + extra Field + Composite Primary Key
body: <p>I'm trying to realise this table structure:</p>
  
  <p><a href="http://www.img-teufel.de/uploads/Unbenannt1d4f8a349jpg.jpg" rel="nofollow">http://www.img-teufel.de/uploads/Unbenannt1d4f8a349jpg.jpg</a></p>
  
  <p>(not embedded due to size)</p>
  
  <p>It creates my classes how i want, except there's no composite primary key in tbl_license_user_alerts.</p>
  
  <p>So, how to create one in my case?</p>
  
  <p>tbl_license:</p>
  
  <pre><code>@Entity
  @Table( name = "tbl_license" )
  public class License implements Serializable
  {
    private static final long serialVersionUID = 1L;
  
    @Id
    @GeneratedValue
    @Column( name = "id", nullable = false, columnDefinition = "serial" )
    private int id;
  
    @ElementCollection
    @JoinTable( name = "tbl_license_user_alert" )
    private List&lt;LicenseUserAlert&gt; alerts;
  
    public int getId()
    {
      return id;
    }
  
    public void setId( int id )
    {
      this.id = id;
    }
  
    public List&lt;LicenseUserAlert&gt; getAlerts()
    {
      return alerts;
    }
  
    public void setAlerts( List&lt;LicenseUserAlert&gt; alerts )
    {
      this.alerts = alerts;
    }
  }
  </code></pre>
  
  <p>tbl_user:</p>
  
  <pre><code>@Entity
  @Table( name = "tbl_user" )
  public class User implements Serializable
  {
    private static final long serialVersionUID = 1L;
  
    @Id
    @GeneratedValue
    @Column( name = "id", nullable = false, columnDefinition = "serial" )
    private int id;
  
    @ElementCollection
    @JoinTable( name = "tbl_license_user_alert" )
    private List&lt;LicenseUserAlert&gt; alerts;
  
    public int getId()
    {
      return id;
    }
  
    public void setId( int id )
    {
      this.id = id;
    }
  
    public List&lt;LicenseUserAlert&gt; getAlerts()
    {
      return alerts;
    }
  
    public void setAlerts( List&lt;LicenseUserAlert&gt; alerts )
    {
      this.alerts = alerts;
    }
  }
  </code></pre>
  
  <p>tbl_license_user_alert:</p>
  
  <pre><code>@Embeddable
  public class LicenseUserAlert implements Serializable
  {
    private static final long serialVersionUID = 1L;
  
    @ManyToOne
    @JoinColumn( name = "license_id"/*, columnDefinition = "int"*/ )
    private License license;
  
    @ManyToOne
    @JoinColumn( name = "user_id"/*, columnDefinition = "int"*/ )
    private User user;
  
    @Column( name = "timer", nullable = false, columnDefinition = "int default 86400" )
    private int timer = 86400
  
    public License getLicense()
    {
      return license;
    }
  
    public void setLicense( License license )
    {
      this.license = license;
    }
  
    public User getUser()
    {
      return user;
    }
  
    public void setUser( User user )
    {
      this.user = user;
    }
  
    public int getTimer()
    {
      return timer;
    }
  
    public void setTimer( int timer )
    {
      this.timer = timer;
    }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>@danny.lesnik
  Then I'm getting an exception:
  org.hibernate.AnnotationException: com.example.my.entities.license.LicenseUserAlertId must not have @Id properties when used as an @EmbeddedId: com.example.my.entities.user.User.alerts.collection&amp;&amp;element.id</p>
  
  <pre><code>@Embeddable
  public class LicenseUserAlert implements Serializable
  {
    private static final long serialVersionUID = 1L;
  
    @EmbeddedId
    private LicenseUserAlertId id;
  
    @Column( name = "timer", nullable = false, columnDefinition = "int default 86400" )
    private int timer = 86400;
  
    public LicenseUserAlertId getId()
    {
      return id;
    }
  
    public void setId( LicenseUserAlertId id )
    {
      this.id = id;
    }
  
    public License getLicense()
    {
      return id.getLicense();
    }
  
    public void setLicense( License license )
    {
      id.setLicense( license );
    }
  
    public User getUser()
    {
      return id.getUser();
    }
  
    public void setUser( User user )
    {
      id.setUser( user );
    }
  
    public int getTimer()
    {
      return timer;
    }
  
    public void setTimer( int timer )
    {
      this.timer = timer;
    }
  }
  </code></pre>
  
  <p>IdClass:</p>
  
  <pre><code>@Embeddable
  public class LicenseUserAlertId implements Serializable
  {
    private static final long serialVersionUID = 1L;
  
    @ManyToOne
    @JoinColumn( name = "license_id"/*, columnDefinition = "int"*/ )
    private License license;
  
    @ManyToOne
    @JoinColumn( name = "user_id"/*, columnDefinition = "int"*/ )
    private User user;
  
    public License getLicense()
    {
      return license;
    }
  
    public void setLicense( License license )
    {
      this.license = license;
    }
  
    public User getUser()
    {
      return user;
    }
  
    public void setUser( User user )
    {
      this.user = user;
    }
  }
  </code></pre>