title: hibernate optional join - need to return join column value either way
body: <p>as a follow on from <a href="https://stackoverflow.com/questions/5231579/hibernate-optional-join">hibernate optional join</a></p>
  
  <p>Many existing queries in the system rely on there being a mapped relationship between EntityOne and EntityTwo and changing this is not an attactive option, however I now need to start storing and retrieving values in <code>tbl_one.two_id</code> which do not exist in the joined table. On the way in, this works fine with the following mapping:</p>
  
  <pre><code>@Entity
  @Table(name="tbl_one")
  public class EntityOne
  {
      ....
      @ManyToOne
      @NotFound(action = NotFoundAction.IGNORE)
      @JoinColumn(name = "two_id")
      private EntityTwo two;
      ....
  }
  </code></pre>
  
  <p>.. However when querying, if a result has a value for <code>two_id</code> which does not exist in the joined table, the value for the field <code>two</code> is null and I have no way of telling what the value for <code>tbl_one.two_id</code> actually is. I was hoping hibernate would create a dummy <code>EntityTwo</code> object and populate it's id field only if the relationship did not exist.</p>
  
  <p>I've also tried mapping <code>two_id</code> to another field without a join:</p>
  
  <pre><code>@Entity
  @Table(name="tbl_one")
  public class EntityOne
  {
      ....
      @ManyToOne
      @NotFound(action = NotFoundAction.IGNORE)
      @JoinColumn(name = "two_id", insertable = false, updatable = false)
      private EntityTwo two;
  
      @Column(name = "two_id")
      private String twoId;
      ....
  }
  </code></pre>
  
  <p>... but then the <code>two</code> field is never populated, even when the corresponding record exists.</p>
  
  <p>Essentially I need some way of inserting/updating a value for <code>tbl_one.two_id</code> and retrieving a joined entity for <code>EntityTwo</code> if the relationship exists, or just the value for <code>tbl_one.two_id</code> if the relationship does not exist.</p>
  
  <p>I'm hoping to be able to solve this without creating two mappings for the same table.</p>
  
  <p>thanks.</p>
  
  <p><strong>EDIT:</strong> more info</p>
  
  <pre><code>public class EntityTwo
  {
      ...
      @Id
      @Column(name = "site_id")
      private String id;
      ...
  }
  </code></pre>