title: Hibernate/JPA 2.0 @OneToOne relationship with possibly non existent matching rows
body: <p>I have two tables, let's say <strong>Person and Person_info</strong>. Both tables have <strong>shared PK person_id</strong> which is also used as a Foreign key.
  The situation is, there can exist Person for which doesn't exist Person_info. <strong>Person_info is optional for person</strong>, there does not need to exist row with the same person_id.
  When I want to search for persons with hibernate, e.g.:</p>
  
  <pre><code>Criteria criteria = session.createCriteria(Person.class);
  criteria.add(Restrictions.eq("name", name));
  Object obj = criteria.uniqueResult();
  </code></pre>
  
  <p>I get an exception:</p>
  
  <pre><code> [&lt;package&gt;.PersonInfo#338664] org.hibernate.ObjectNotFoundException: No row with the given identifier exists:  &lt;package&gt;.PersonInfo#338664]
  </code></pre>
  
  <p>Of course Person_info row with person_id=338664 really doesn't exist. But I still would like hibernate to be able to handle this. <strong>If the row exists, then I want to load it, if it doesn't I want to get null</strong>, not an exception.</p>
  
  <p>I am using hibernate version 3.5.6-Final. 
  I think this thread is exactly about the same problem, but there is no solution in it <a href="https://stackoverflow.com/questions/2001007/primarykeyjoincolumn-with-bidirectional-onetoone-relationship">@PrimaryKeyJoinColumn with Bidirectional @OneToOne relationship</a></p>
  
  <p>Thank you for your help.</p>
  
  <p>The annotated classes look like this:</p>
  
  <p>Person class:</p>
  
  <pre><code>@Entity
  @Table(name="Person")
  public class Person extends BaseDBEntity&lt;Integer&gt;{
      private PersonInfo personInfo;
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name="person_id")
      public Integer getId() {
        return id;
      }
  
      @OneToOne(cascade = CascadeType.ALL, optional = true)
      @PrimaryKeyJoinColumn
      public PersonInfo getPersonInfo() {
        return personInfo;
      }
  }
  </code></pre>
  
  <p>PersonInfo class:</p>
  
  <pre><code>@Entity
  @Table(name="Person_info")
  public class PersonInfo extends BaseDBEntity&lt;Integer&gt; {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name="person_id")
      public Integer getId() {
        return id;
      }
  }  
  </code></pre>
  
  <p>Both of them inherit from BaseDBEntity:</p>
  
  <pre><code>public abstract class BaseDbEntity&lt;T&gt; implements Serializable {
  
        private static final long serialVersionUID = 1L;
  
        protected T id;
  }
  </code></pre>