title: Hibernate one-to-one XML mapping
body: <p>I found the following links:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/5973509/hibernate-one-to-one-mapping-problem">Hibernate one to one mapping problem</a></li>
  <li><a href="https://stackoverflow.com/questions/14682169/hibernate-one-to-one-mapping-with-a-reference-column-xml-mapping">Hibernate one-to-one mapping with a reference column (XML mapping)</a></li>
  <li><a href="https://stackoverflow.com/questions/13877338/hibernate-one-to-one-on-foreign-key-vs-one-to-one-on-primary-key">hibernate one-to-one (on foreign key) vs one-to-one (on primary key)</a></li>
  </ul>
  
  <p>But nothing seems to work.</p>
  
  <p>I have 2 entities:</p>
  
  <pre><code>class User {
      Integer userId;
      Profile userProfile;
  }
  
  class Profile {
      Integer profileId;
      User user;
  }
  </code></pre>
  
  <p>With XML mapping:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="model.User" table="User" catalog="Proj1" dynamic-update="true"&gt;
          &lt;id name="userId" type="java.lang.Integer"&gt;
              &lt;column name="userId" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;one-to-one name="userProfile" class="model.Profile"&gt;
          &lt;/one-to-one&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  &lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  &lt;!-- Generated Jun 12, 2013 7:51:22 PM by Hibernate Tools 3.4.0.CR1 --&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="model.Profile" table="Profile" catalog="Proj1" dynamic-update="true"&gt;
          &lt;id name="profileId" type="java.lang.Integer"&gt;
              &lt;column name="profileId" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;many-to-one name="user" class="model.Users" unique="true"&gt;
              &lt;column name="userId" /&gt;
          &lt;/many-to-one&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>The thing here is, the <code>User</code> must have exactly one <code>Profile</code> but the <code>Profile</code> doesn't necessarily to have one <code>User</code> so a <code>Profile</code> may have <code>null</code> <code>User</code>.</p>
  
  <p>Now the problem is every time I fetch a <code>User</code> with its associated <code>Profile</code>, the <code>Profile</code> retrieved is the <code>Profile</code> with the <code>profileId</code> the same as <code>userId</code>, that is if the <code>User</code> has <code>userId</code> 4 the <code>Profile</code> retrieved is the profile with <code>profileId</code> 4 as well even though it is supposed to retrieve <code>Profile</code> with <code>userId</code> 4 not <code>profileId</code> 4.</p>
  
  <p><strong>Update: add Dao code</strong></p>
  
  <pre><code>public User findById( int id ) {
      log.debug("getting User instance with id: " + id);
      try {
          Criteria userCriteria = this.sessionFactory.getCurrentSession().createCriteria(User.class);
          userCriteria.add(Restrictions.idEq(id));
  
          userCriteria.setFetchMode("userProfile", FetchMode.JOIN);
  
          userCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
          Users instance = (Users) userCriteria.uniqueResult();
  
          if(instance == null)
              log.debug("get successful, no instance found");
          else
              log.debug("get successful, instance found");
          return instance;
      }
      catch(RuntimeException re) {
          log.error("get failed", re);
          throw re;
      }
  }
  </code></pre>