title: 'Cannot add or update a child row: a foreign key constraint fails' when trying to add a primary composite key in Hibernate
body: <p>I'm working on the project of a school gradesbook implementation where there are following entities: students, subjects and grades. Each grade has one subject, one student, date and value. I'm trying to implement this relation and I decided for a grade to have a composite primary key that is composed of subject id, student id and date, here are my entity classes</p>
  
  <p>Student:</p>
  
  <pre><code>@Entity
  @Table(name="Students")
  public class Student {
       @Id @Column(name="Student_Id")
       private int studentId;
       @Column(name="First_Name")
       private String firstName;
       @Column(name="Last_Name")
       private String lastName;
       //getters and setters
  }
  </code></pre>
  
  <p>Subject:</p>
  
  <pre><code>@Entity
  @Table(name="Subjects")
  public class Subject {
      @Id @Column(name="Subject_Id")
      private int subjectId;
      @Column(name="Subject_Name")
      private String subjectName;
      //getters and setters
  }
  </code></pre>
  
  <p>Grade (Here I created the composite primary key by adding <code>@Id</code> tag on <code>Student_Id</code>,<code>Subject_Id</code> and <code>Date</code>, where <code>Student_Id</code> and <code>Subject_Id</code> are also foreign keys):</p>
  
  <pre><code>@Entity
  @Table(name="Grades")
  public class Grade implements Serializable{
      @Id
      @ManyToOne @JoinColumn(name="Student_Id", foreignKey=@ForeignKey(name="FK_Grades_Student"))
      @Cascade(CascadeType.SAVE_UPDATE)
      private Student student;
      @Id
      @ManyToOne @JoinColumn(name="Subject_Id", foreignKey=@ForeignKey(name="FK_Grades_Subject"))
      @Cascade(CascadeType.SAVE_UPDATE)
      private Subject subject;
      @Column(name="Date")
      @Id
      private Date date;
      @Column(name="Letter_Value") //A,B,C,D,F
      private String letterValue;
      //getters and setters
  }
  </code></pre>
  
  <p>And here is how I run it from <code>Main</code>:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
          Subject subject = new Subject(); subject.setSubjectId(1); subject.setSubjectName("Biology");
          Student student = new Student(); student.setStudentId(1); student.setFirstName("Jack"); student.setLastName("Smith");
          Grade grade = new Grade(); grade.setStudent(student); grade.setSubject(subject); grade.setDate(new Date()); grade.setLetterValue("B"); 
  
          SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          Session session = sessionFactory.openSession();
          session.beginTransaction();
          session.save(grade);
          session.getTransaction().commit();
          session.close();
          sessionFactory.close();
      }
  }
  </code></pre>
  
  <p>After running I get the following exception:</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
  
  Cannot add or update a child row: a foreign key constraint fails (`grades_book_db`.`grades`, CONSTRAINT `FK_Grades_Subject` FOREIGN KEY (`Subject_Id`) REFERENCES `subjects` (`Subject_Id`))
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
      at com.mysql.jdbc.Util.getInstance(Util.java:408)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
      at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
      at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
      at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
      ... 18 more
  </code></pre>
  
  <p>How to fix this exception, and how can I implemented the composite key I mentioned above? I am using MySQL DBMS.</p>