title: JPA 2: Referential integrity constraint violation while saving entity with undirectional OneToMany relationship
body: <p>I'm working on database design and Java implementation of some sports event model.
  I have two entities: <code>Game</code> and <code>Participant</code> and undirectional OneToMany relationship between them. <code>Game</code> can has many <code>Participant</code>'s. </p>
  
  <p>In database it looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/tqSIi.png" alt="enter image description here">
  Of course i have FK constraint on table <code>event_participant</code>:</p>
  
  <pre><code>CONSTRAINT `FK_par_eve_eve_id__eve_id`
      FOREIGN KEY (`event_id`)
      REFERENCES `event` (`id`));
  </code></pre>
  
  <p>In Java code i wrote this mapping (simplified):</p>
  
  <pre><code>@Entity
  @Table(name = "event")
  public class GameEntity extends AbstractPersistable&lt;Long&gt; implements Game {
      @Nullable
      @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "game")
      private List&lt;ParticipantEntity&gt; participants;
  }
  
  @Entity
  @Table(name = "event_participant")
  public class ParticipantEntity extends AbstractPersistable&lt;Long&gt; implements GameParticipant {
      @ManyToOne
      @JoinColumn(name="event_id", referencedColumnName = "id", insertable = true)
      private GameEntity game;
  }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>TeamEntity teamHome = teamsDao.findOne(1L);
  TeamEntity teamGuest = teamsDao.findOne(2L);
  GameEntity newEntity = new GameEntity()
          .addParticipant(teamHome, GameParticipant.Alignment.HOME)
          .addParticipant(teamGuest, GameParticipant.Alignment.GUEST);
  
  dao.save(newEntity);
  
  public GameEntity addParticipant(TeamEntity team, GameParticipant.Alignment alignment) {
      if (participants == null) {
          participants = new ArrayList&lt;ParticipantEntity&gt;();
      }
      participants.add(ParticipantEntity.create(team, alignment));
      return this;
  }
  
  public static ParticipantEntity create(TeamEntity team, Alignment alignment) {
      ParticipantEntity object = new ParticipantEntity();
      object.team = team;
      object.alignment = alignment;
      return object;
  }
  </code></pre>
  
  <p>When i'm trying to save the new <code>GameEntity</code> object which contains just created <code>Participant</code>'s objects i expect that Hibernate creates one new record in <code>event</code> table and related records in <code>event_participant</code> table.</p>
  
  <p>But i got an exception:</p>
  
  <pre><code>Referential integrity constraint violation: "FK_PAR_EVE_EVE_ID__EVE_ID: PUBLIC.EVENT_PARTICIPANT FOREIGN KEY(EVENT_ID) REFERENCES PUBLIC.EVENT(ID) (0)"; SQL statement:
  insert into event_participant (id, alignment, event_id, participant_id, participant_type) values (null, ?, ?, ?, ?)
  </code></pre>
  
  <p>This insert query contains NULL value for <code>event_id</code> field. I don't understand why.
  When i removed DB constraint everything is ok and <code>event_id</code> column has the correct value.</p>
  
  <p>All the boilerplate is done by Spring Data util classes.
  I'm using JPA 2.0, mysql-connector-java 5.1.28, hibernate-entitymanager 4.0.1.Final, spring-data-jpa 1.3.4.RELEASE and H2 database for testing. 
  I got the same result using either H2 or MySQL.</p>
  
  <p>Where i failed?</p>