title: Hibernate OneToOne: Forced to chose between optional = false or cascade?
body: <p>In order to keep transfered data small I created two entities for my files in the database. The <code>fileheader</code> to keep some general information about the files and the <code>fileblob</code>, including <code>fileId</code> and the blob. Often, I only need to ask for general <code>fileinformations</code>.</p>
  
  <p>So I need to load the <code>fileblobs</code> lazily.</p>
  
  <p>As I learned in <a href="https://stackoverflow.com/questions/43678271/hibernate-is-loading-lazy-objects-without-being-asked-for">this discussion</a> and <a href="https://stackoverflow.com/questions/1444227/making-a-onetoone-relation-lazy">that discussion.</a> this could be achieved with <code>optional = false</code>. It works perfect to load the <code>fileblobs</code> lazily. Unfortunately it affects save by cascade.</p>
  
  <p>So here is my attribute in the <code>Fileh.class</code> for the blob:</p>
  
  <pre><code>@OneToOne(mappedBy = "fileh", targetEntity = Fileblob.class, fetch = FetchType.LAZY, optional = false)
  @org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK })
  private Fileblob fileblob;
  </code></pre>
  
  <p>If I now save a <code>fileh</code> with attached <code>fileblob</code>, this error is thrown:</p>
  
  <blockquote>
    <p>org.hibernate.id.IdentifierGenerationException: null id generated
    for:class Fileblob</p>
  </blockquote>
  
  <p>if i switch from id-generation strategy "identity" to "increment" this error is thrown:</p>
  
  <blockquote>
  <pre><code>ERROR SqlExceptionHelper:147 - Cannot add or update a child row: a foreign key constraint fails (`CORE`.`FILEBLOB`, CONSTRAINT
  </code></pre>
    
    <p><code>FKFILEBLOB412557</code> FOREIGN KEY (<code>ID</code>) REFERENCES <code>FILEH</code> (<code>ID</code>))
        Query is: insert into CORE.<code>FILEBLOB</code> (<code>FILEBLOB</code>, <code>ID</code>) values (?, ?)</p>
  </blockquote>
  
  <p>So there is a problem with generating the id...
  If i now turn off save by cascade my attribute looks like this.</p>
  
  <pre><code>@OneToOne(mappedBy = "fileh", targetEntity = Fileblob.class, fetch = FetchType.LAZY, optional = false)
  private Fileblob fileblob;
  </code></pre>
  
  <p>In order to save now, I have to call</p>
  
  <pre><code>persistentSession.saveOrUpdate(fileh);
  persistentSession.saveOrUpdate(fileblob);
  </code></pre>
  
  <p>Is this not just what <code>CascadeType.SAVE_UPDATE</code> is supposed to do?
  Why is this working for "manual cascading" but not automatically?</p>
  
  <p>P.s.: To complete my example here the counterpart in <code>fileblob.class</code></p>
  
  <pre><code>@PrimaryKeyJoinColumn   
  @OneToOne(targetEntity=Fileh.class, fetch=FetchType.LAZY)   
  @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
  @JoinColumns({ @JoinColumn(name="`ID`", referencedColumnName="`ID`", unique=true, nullable=false) })    
  private Fileh fileh;
  
  @Column(name="`ID`", nullable=false, insertable=false, updatable=false, unique=true)    
  @Id 
  @GeneratedValue(generator="FILEBLOB_FILEHID_GENERATOR") 
  @org.hibernate.annotations.GenericGenerator(name="FILEBLOB_FILEHID_GENERATOR", strategy="foreign", parameters=@org.hibernate.annotations.Parameter(name="property", value="fileh")) 
  private int filehId;
  </code></pre>