title: Master/Detail relationship with auto increment. Is it possible to persist them at once?
body: <p>I have this scenario :</p>
  
  <pre><code>tbl_master(
    master_field_pk int(11) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`master_field_pk`)
  )
  </code></pre>
  
  <p>and</p>
  
  <pre><code>tbl_detail(
    `detail_field_pk` int(11) NOT NULL AUTO_INCREMENT,
    `master_field_pk` int(11) DEFAULT NULL,
    CONSTRAINT `child_fk1` FOREIGN KEY (`master_field_pk`) REFERENCES `tbl_master` (`master_field_pk`) ON DELETE NO ACTION ON UPDATE NO ACTION,
    PRIMARY KEY (`master_field_pk`)
  )
  </code></pre>
  
  <p>This is my hibernate class for master :</p>
  
  <pre><code>@Entity
  @Table(name = "tbl_master")
  @Name("TblMaster")
  public class TblMaster implements Serializable{
    @Id
    @Column(name = "master_field_pk")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer MasterFieldPk;
  
    @Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN})
    @OneToMany(mappedBy="tblMaster")
    @JoinColumn(name="master_field_pk", insertable=true, updatable=true, referencedColumnName="master_field_pk")
    private Set&lt;TblDetail&gt; tblDetails;
  
    @Transient
    private List&lt;TblDetail&gt; tblDetailsList;  
  
    // any other things
  }
  </code></pre>
  
  <p>and this one is for detail :</p>
  
  <pre><code>@Entity
  @Table(name="tbl_detail")
  @Name("TblDetail")
  public class TblDetail implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="detail_field_pk")
    private Integer detailFieldPk;
  
    @Column(name="master_field_pk")
    private Integer MasterFieldPk;
  
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="master_field_pk", insertable = false, updatable = false, referencedColumnName = "master_field_pk")
    private TblMaster tblMaster;
  
    // any other things
  }
  </code></pre>
  
  <p>To insert, i use this block of code (more or less) :</p>
  
  <pre><code>TblDetail detail_1 = ...
  detail_1.setTblMaster(tblMaster);
  // and so on ..
  
  Set&lt;TblDetails&gt; set = ...
  set.add(detail_1);
  // and so on...
  
  tblMaster.setTblDetails(set);
  em.persist(tblMaster);
  em.flush();
  </code></pre>
  
  <p>All the records (the master record and the details record) are perfectly inserted. However, the <code>master_field_pk</code> in <code>tbl_detail</code> - the foreign key - is still null. Is it possible to avoid this behavior? Thanks</p>