title: Hibernate one to zero or one mapping. Exception: attempted to assign id from null one-to-one property?
body: <p>I do as @Tony answered in <a href="https://stackoverflow.com/questions/841354/hibernate-one-to-zero-or-one-mapping">Hibernate one to zero or one mapping</a>.</p>
  
  <p>I have a class <code>FileMeta</code>.</p>
  
  <pre><code>@Entity
  @Inheritance
  @DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING, length = 30)
  @DiscriminatorValue("FileMeta")
  public class FileMeta {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      protected long id;
  
      ...SOME ATTRIBUTES...
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "FK_GARBAGE")
      @NotFound(action = NotFoundAction.IGNORE)
      protected Garbage garbage;
  
      @Enumerated(EnumType.ORDINAL)
      private FileT type;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "FK_SHARE")
      @NotFound(action = NotFoundAction.IGNORE)
      private Share share;
  
      ...METHODS...
  }
  </code></pre>
  
  <p>And a class <code>Share</code>.</p>
  
  <pre><code>@Entity
  public class Share {
      @Id
      @GeneratedValue(generator = "shareForeignGenerator")
      @GenericGenerator(
              name = "shareForeignGenerator",
              strategy =  "foreign",
              parameters = @Parameter(name = "property", value = "fileMeta")
      )
      private Long id;
  
      ...SOME ATTRIBUTES...
  
      @OneToOne(mappedBy = "share")
      @PrimaryKeyJoinColumn
      @NotFound(action = NotFoundAction.IGNORE)
      private FileMeta fileMeta;
  
      ...METHODS...
  }
  </code></pre>
  
  <p>And when I tried to fill my <code>FileMeta</code> with <code>Share</code>:</p>
  
  <pre><code>    Share share = new Share();
      share.setFileMeta(fileMeta);
      fileMeta.setShare(share);
      fileMetaRepository.save(fileMeta);
  </code></pre>
  
  <p>I received exception: <code>attempted to assign id from null one-to-one property</code></p>
  
  <p>I followed into <code>Hibernate</code>. And noticed that, in <code>generator</code> method, the <code>associatedObject</code> is not my given <code>Share</code> object at all, but a new <code>Share</code> object instantiated by a <code>EventSource</code></p>
  
  <p>In <code>DefaultMergeEventListener.java</code>, </p>
  
  <pre><code>    if ( copyCache.containsKey( entity ) ) {
          persister.setIdentifier( copyCache.get( entity ), id, source );
      }
      else {
          ( (MergeContext) copyCache ).put( entity, source.instantiate( persister, id ), true ); //before cascade!
      }
  </code></pre>
  
  <p>The <code>copyCache</code> sensed that <code>entity</code> (i.e my given <code>Share</code> object) is not in <code>copyCache</code>, and instantiated a new <code>Share</code> object, which doesn't have a <code>FileMeta</code> reference obviously.</p>
  
  <p>I've completely confused.</p>