title: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
body: <p>I'm constantly getting error when i wanna insert some data</p>
  
  <p><strong>Project Class</strong></p>
  
  <pre><code>@Entity
  @NamedQueries({
          @NamedQuery(name = "Project.findAll",
           query = "SELECT p FROM Project p"),
          @NamedQuery(name = "Project.findByTitle",
                  query = "SELECT p FROM Project p WHERE p.title = :title")
  })
  public class Project implements Serializable{
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int projectId;
  
      @Column
      private String title;
      @Column
      private String description;
      @Column
      private Date projectDate;
  
      @ManyToOne
      @JoinColumn(name = "projectStatusId")
      private ProjectStatus projectStatus;
  
      @OneToMany(mappedBy = "projectMemberId",fetch = FetchType.EAGER)
      private List&lt;ProjectMember&gt; memberList = new ArrayList&lt;ProjectMember&gt;();
  
      /**
       * Setters and Getters
       */
  }
  </code></pre>
  
  <p><strong>ProjectMember Class</strong></p>
  
  <pre><code>@Entity
  @NamedQueries({
          @NamedQuery(name = "ProjectMember.findAll",
                  query = "SELECT pm FROM ProjectMember pm"),
          @NamedQuery(name = "ProjectMember.findByProject",
                  query = "SELECT pm FROM ProjectMember pm WHERE pm.project = :project"),
          @NamedQuery(name = "ProjectMember.findByUser",
                  query = "SELECT pm FROM ProjectMember pm WHERE pm.user = :user"),
          @NamedQuery(name = "ProjectMEmeber.findByUserAndProject",
                  query = "SELECT pm FROM ProjectMember pm  WHERE pm.user = :user AND pm.project = :project")
  })
  
  public class ProjectMember implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int projectMemberId;
  
  
      @Column
      private Date activationDate;
      @Column
      private Date lastActiveDate;
  
      @ManyToOne
      @JoinColumn(name = "memberStatusId")
      private MemberStatus memberStatus;
  
      @ManyToOne
      @JoinColumn(name = "projectId")
      private Project project;
  
  
      @ManyToOne
      @JoinColumn(name = "memberRoleId")
      private MemberRole memberRole;
  
      @ManyToOne
      @JoinColumn(name = "userId")
      private User user;
  
      /**
       * Setter and Getter
       */
  }
  </code></pre>
  
  <p>that's how i run it</p>
  
  <p><strong>ProjectController Class</strong></p>
  
  <pre><code>public String createProject(){
      project.setProjectDate(new Date());
      project.setProjectStatus(new ProjectStatusFacade().findByStatus("active"));
  
      ProjectMember projectMember = new ProjectMember();
      projectMember.setMemberStatus(new MemberStatusFacade().findByStatus("accepted"));
      projectMember.setMemberRole(new MemberRoleFacade().findByRole("team leader"));
      projectMember.setActivationDate(new Date());
      projectMember.setUser(userSession.getUser());
  
      new ProjectFacade().create(project);
      System.out.print(project);
      projectMember.setProject(project);
      new ProjectMemberFacade().create(projectMember);
  
      userSession.setUser(new UserFacade().findById(userSession.getUser().getUserId()));
      return "index?faces-redirect=true";
  }
  </code></pre>
  
  <p>and that's what i get in the end</p>
  
  <pre><code> Caused by: javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
          ... more exceptions
  
      Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
          ... more exceptions
  
      Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
          ...more exceptions
  
      Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`goent`.`projectmember`, CONSTRAINT `FKfyw2iinfhhsbrmqbu1sr7l93q` FOREIGN KEY (`projectMemberId`) REFERENCES `project` (`projectId`))
          ... more exceptions
  
      15:51:06,449 ERROR [io.undertow.request] (default task-4) UT005023: Exception handling request to /hibernate/pages/project-add-member.xhtml: javax.servlet.ServletException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
          ... more exceptions
  
      Caused by: javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
          ... more exceptions
  
      Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
          ...more exceptions
      Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
          ... more exceptions
  
      Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`goent`.`projectmember`, CONSTRAINT `FKfyw2iinfhhsbrmqbu1sr7l93q` FOREIGN KEY (`projectMemberId`) REFERENCES `project` (`projectId`))
          ... more exceptions
  </code></pre>
  
  <p>Using: Java, JPA, Hibernate, MySQL5.7</p>