title: Cannot add or update a child row: a foreign key constraint fails - Bidirectional mapping in hibernate
body: <p>Hi I have a problem with JPA...</p>
  
  <p>DB:</p>
  
  <p><img src="https://i.stack.imgur.com/Ho61k.png" alt="Database"></p>
  
  <p>Java classes (irrelevant fields are ommited):</p>
  
  <p>User:</p>
  
  <pre><code>@Entity
  public class User{
      @Id
      private int iduser;
  
      //bi-directional many-to-one association to UserInfo
      @OneToMany(mappedBy="user", cascade= {CascadeType.ALL}, fetch=FetchType.EAGER)
      private List&lt;UserInfo&gt; userInfos;
  }
  </code></pre>
  
  <p>UserInfo:</p>
  
  <pre><code>@Entity
  @Table(name="user_info")
  public class UserInfo {
      @Id
      @Column(name="iduser_info")
      private int iduserInfo;
  
      //bi-directional many-to-one association to User
      @ManyToOne  
      private User user;
  }
  </code></pre>
  
  <p>Currently when I try to do this (again I omitted setting irrelevant fields):</p>
  
  <pre><code>User u = new User();            
  UserInfo info = new UserInfo();
  u.addUserInfo(info);
  
  em.persist(u); // save user
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>Cannot add or update a child row: a foreign key constraint fails (`webstore`.`user_info`, CONSTRAINT `fk_user_info_user` FOREIGN KEY (`user_iduser`) REFERENCES `user` (`iduser`) ON DELETE NO ACTION ON UPDATE NO ACTION)
  </code></pre>
  
  <p>I have been banging my head all day and I can't figure it out... I have also searched for solutions here but they all suggest that this error shows that I want to enter UserInfo without user_iduser value entered, but even if I add this before persist:</p>
  
  <pre><code>info.setUser(u);
  </code></pre>
  
  <p>it still doesn't work - is bidirectional mapping even supported with cascading? The desired effect is that User should be inserted and then all the UserInfos in the list after it refering the original User. How can I achieve that?</p>
  
  <p>I don't want to do </p>
  
  <pre><code>SET foreign_key_checks = 0/1;
  </code></pre>
  
  <p>everytime =(</p>