title: Hibernate, mapping of foreign keys that have been generated by a sequence in the parent table
body: <p>I have a 1:n relation in the database and java objects that represent the relation with hibernate.
  The primary key of the parent table is generated with a database sequence,
  the child object uses the primary key of the parent object as foreign key.</p>
  
  <p>I create a new parent object including a set of new child objects.
  Then I try to persist all objects with <strong>one single</strong> </p>
  
  <pre><code>session.save(contract);
  </code></pre>
  
  <p>The storage of the parent object works without problems, but as soon as I add child objects I get an exception </p>
  
  <p>... not-null property references a null or transient value ...</p>
  
  <p>because of the missing FK in the child object.</p>
  
  <p>Is there a mapping which tells hibernate to use the newly generated PK of the parent object as FK in the child objects, or do I have to persist the child objects separately with the FK manually set?</p>
  
  <p>mapping parent object:
  ...</p>
  
  <pre><code>@Id 
  @Column(name="ID", unique=true, nullable=false, precision=22, scale=0)
  @SequenceGenerator(name="sequence_seq", sequenceName="SEQ_CONTRACT")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_seq")
  public long getId() {
      return this.id;
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="contract")
      public Set&lt;ContractSearchtags&gt; getContractSearchtagses() {
          return this.contractSearchtagses;
      }
  </code></pre>
  
  <p>...</p>
  
  <p>mapping of child object</p>
  
  <p>...</p>
  
  <pre><code>@ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="CONTRACT_ID", nullable=false)
      public Contract getContract() {
          return this.contract;
      }
  </code></pre>
  
  <p>...</p>