title: integrity constraint violated - parent key not found - OneToOne and ManyToOne
body: <p>Facing issue inserting an entity into database. I am using Hibernate JPA.</p>
  
  <p>I have tables Reminder and ReminderAction and Each Reminder will hold current status of ReminderAction (ie. OneToOne) and end of transaction each Reminder will have many ReminderActions (with different status).</p>
  
  <pre><code>DB Table columns are
  
  REMINDER --&gt; REMINDER_ID (PK), CURRENT_ACTION_ID (FK)...
  REMINDER_ACTION --&gt; REMINDER_ACTION_ID (PK), REMINDER_ID (FK)...
  
  
  Reminder Entity
  ---------------------
  
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "REMINDER_SEQ_STORE")
  @Column(name = "REMINDER_ID", unique = true, nullable = false, precision = 22, scale = 0)
  public Integer getReminderId() {
  return reminderId;
  }
  
  
  
  @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = ReminderAction.class)
  @JoinColumn(name = "CURRENT_ACTION_ID")
  public ReminderAction getCurrentAction() {
  return currentAction;
  }
  
  
  ReminderAction Entity
  ---------------------
  
  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="REMINDER_ACTION_SEQ_STORE")
  @Column(name = "REMINDER_ACTION_ID", unique = true, nullable = false)
  public Integer getReminderActionId() {
  return reminderActionId;
  }
  
  
  @ManyToOne(cascade=CascadeType.ALL,targetEntity=Reminder.class)
  @JoinColumn(name="REMINDER_ID")
  public Reminder getReminder() {
  return reminder;
  }
  
  
  DAO
  ---
  
  public void saveOrUpdate(final E transientObject) {
  getCurrentSession().saveOrUpdate(transientObject);
  getCurrentSession().flush();
  }
  </code></pre>
  
  <p>During saveOrUpdate, hibernate gets the sequence for Reminder and ReminderAction from DB</p>
  
  <pre><code>Hibernate: select REMINDER_SEQ.nextval from dual
  Hibernate: select REMINDER_ACTION_SEQ.nextval from dual
  </code></pre>
  
  <p>But during flush(), <strong>hibernate trying to insert ReminderAction first</strong> and due to ReminderID in ReminderAction is null its throwing <strong>ORA-02291: integrity constraint (REMINDER_ACTION_FK) violated - parent key not found</strong></p>
  
  <p>Refered below threads and problem because of trigger in DB. <strong>But I dont have any triggers for Reminder and Reminder_Action table in DB.</strong></p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/5715184/jpa-bidirectional-one-to-many-foreign-key-issues">JPA Bidirectional One to Many Foreign Key Issues</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/15332930/integrity-constraint-violated-just-when-i-commit-the-transaction-solved">Integrity constraint violated just when I commit the transaction</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/14052631/hibernate-insert-issue-parent-key-not-found">Hibernate: Insert issue - Parent Key not found</a></p></li>
  </ol>
  
  <p><strong>How can I ask hibernate to insert Reminder first then ReminderAction?</strong></p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tried by removing SequenceGenerator from Reminder and ReminderAction and manually set the IDs during saveOrUpdate and end up with same exception</p>
  
  <pre><code>Reminder rem = (Reminder)transientObject;
  rem.getCurrentAction().setReminderId(5);
  rem.getCurrentAction().setReminderActionId(5);
  rem.setReminderId(5);
  
  getCurrentSession().saveOrUpdate(transientObject);
  getCurrentSession().flush();
  
  
  
  Hibernate: insert into REMINDER_ACTION (REMINDER_ID, REMINDER_ACTION_ID) values (?, ?)
  binding parameter [1] as [INTEGER] - 5
  binding parameter [2] as [INTEGER] - 5
  SQL Error: 2291, SQLState: 23000
  ORA-02291: integrity constraint (REMINDER_ACTION_FK) violated - parent key not found
  </code></pre>
  
  <p>Any help is highly appreciated. Thanks.</p>