title: Hibernate Mapping composite primary key that contains foreign keys
body: <p>I have 2 classes Rule and Group. A rule is mapped to multiple groups through another class RuleGroupMap. The corresponding tables in DB are like this</p>
  
  <pre><code>CREATE TABLE `rules` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `validity_start_time` timestamp NULL DEFAULT NULL,
    `validity_end_time` timestamp NULL DEFAULT NULL,
    `rule_name` char(25) DEFAULT '',
    `description` text,
    PRIMARY KEY (`id`)
  )
  
  CREATE TABLE `group` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(200) NOT NULL
    PRIMARY KEY (`id`)
  )
  
  CREATE TABLE `rule_group_map` (
    `area_id` int(11) NOT NULL DEFAULT '0',
    `group_id` bigint(11) NOT NULL DEFAULT '0',
    `active` tinyint(1) DEFAULT NULL,
    PRIMARY KEY (`group_id`,`rule_id`),
    KEY `group` (`group_id`),
    KEY `rule` (`rule_id`),
    CONSTRAINT `group` FOREIGN KEY (`group`) REFERENCES `group` (`id`),
    CONSTRAINT `rule` FOREIGN KEY (`rule_id`) REFERENCES `rules` (`id`)
  )
  </code></pre>
  
  <p>The RuleGroupMap has its PK as a combination of rule_id and group_id. Also, rule_id has a ManyToOne relationship to Rule and group_id has a ManyToOne relationship to Group.</p>
  
  <p>How do I create entities in Hibernate for this scenario?</p>
  
  <p>My current code is as follows and throws a SQLGrammerException.</p>
  
  <pre><code>@Entity
  @Table(name = "rules")
  public class Rule implements Serializable{
  
      @Id @GeneratedValue(strategy = IDENTITY)
      @Getter @Setter @Column(name = "id", nullable = false, unique = true)
      private Long Id;
  
      @Getter @Setter @Column(name = "validity_start_time", nullable = false)
      private Timestamp validity_start;
  
      @Getter @Setter @Column(name = "validity_end_time", nullable = false) 
      private Timestamp validity_end;
  
      @Getter @Setter @Column(name = "rule_name") 
      private String ruleName;
  
      @Getter @Setter @Column(name = "description") 
      private String description;
  
      @Getter @Setter @OneToMany(mappedBy = "rule") 
      private Set&lt;RuleGroupMap&gt; groupsMap;
  }
  
  
  
      @Entity
      @Table(name = "group")
      public class Group implements Serializable {
  
          @Id
          @Getter @Setter @Column(name = "id", nullable = false) 
          private long id;
  
          @Getter @Setter @Column(name = "name", nullable = false) 
          private String name;
  
          @Getter @Setter @OneToMany(mappedBy = "area") 
          private Set&lt;RuleGroupMap&gt; rulesMap;
      }
  
  
  @Entity
  @Table(name = "rule_group_map")
  @Data
  @IdClass(RuleMapPK.class)
  public class RuleGroupMap {
  
      @Id @ManyToOne @JoinColumn(name="group_id", referencedColumnName = "id") private Group group;
  
      @Id @ManyToOne @JoinColumn(name="rule_id", referencedColumnName = "id") private Rule rule;
  
      @Column(name = "active") private boolean active;
  }
  
  @Data
  public class RuleMapPK implements Serializable {
      @Column(name="group_id") long group;
      @Column(name="rule_id") Long rule;
  }
  </code></pre>