title: OneToOne mapping to an Entity from multiple owning entities
body: <p>I am a JPA newbie. I have what I believe is a very simple example, but I am having trouble implementing it. Basically, I have an entity that I want to "reuse" in other entities.</p>
  
  <p>I have a <code>User</code> having two fields - <code>Home</code> and <code>Office</code>. Each of these in turn refers to an <code>Address</code>, like so:</p>
  
  <pre><code>@Entity
  public class User extends Model {
  
      @Column 
      private String name;
  
      @OneToOne
      private Home home;
  
      @OneToOne
      private Office office;
  
  }
  
  @Entity
  public class Home extends Model {
  
      @OneToOne
      private Address address;
  
      @OneToOne(mappedBy="home")
      private User user;
  
      // ...
  
  }
  
  @Entity
  public class Office extends Model {
  
      @OneToOne
      private Address address;
  
      @OneToOne(mappedBy = "office")
      private User user;
  
      // ...
  }
  
  @Entity
  public class Address extends Model {
  
      @Column
      private String line1;
  
      @Column 
      private String line2;
  
      @Column
      private String city;
  
      /*
       * Is this possible? Being owned by two entities?
       */
  
  
      @OneToOne(mappedBy="address")
      private Home home;
  
      @OneToOne(mappedBy="address")
      private Office office;
  
      // ...
  }
  </code></pre>
  
  <p>How do I achieve this?</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I was originally running into this exception:</p>
  
  <pre><code>org.hibernate.AnnotationException: @OneToOne or @ManyToOne on example.Home.address references an unknown entity: example.Address
  </code></pre>
  
  <p>It turns out that one of the classes had imported the <code>@Entity</code> annotation from <code>org.hibernate</code> instead of from <code>javax.persistence</code>. After fixing this, the original exception went away (<a href="http://techazizhk.wordpress.com/2009/01/22/orghibernateannotationexception-onetoone-or-manytoone-on-xxx-references-an-unknown-entity-xxx/#comment-75" rel="nofollow">this</a> helped me pin point the issue)</p>
  
  <p>However, I now have a new exception:</p>
  
  <pre><code>org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: example.User.home -&gt; example.Home
  </code></pre>
  
  <p>I don't quite understand this exactly. Should I call <code>save</code> the <code>Home</code> object before I do so on the <code>User</code> instance?</p>