title: Hibernate JPA one-to-one saving child class entity
body: <p>I have a one-to-one relationship using <code>PrimaryKeyJoinColumn</code> annotated on the parent side. And now I want to save the child entity by itself.</p>
  
  <p>For example, I have <code>Employee</code> and <code>EmpInfo</code> as the child entity, I need to save <code>EmpInfo</code> (of course after setting the id property of the parent to it). However, when such an arrangement is used, I get an exception listed below...</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist
  </code></pre>
  
  <p>Any ideas why hibernate does not allow this? To be more clear, the code I have is below...</p>
  
  <p><strong>ParentEntity:</strong></p>
  
  <pre><code>public class Employee {
      private Long id;
      private String name;
      private EmployeeInfo info;
      private Integer enumId;
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      public Long getId() {
          return id;
      }
  
      @Column(name="EMP_NAME")
      public String getName() {
          return name;
      }
  
      @PrimaryKeyJoinColumn
      @OneToOne(cascade = CascadeType.REMOVE)
      public EmployeeInfo getInfo() {
          return info;
      }
    }
  </code></pre>
  
  <p><strong>ChildEntity:</strong></p>
  
  <pre><code>@Table(name="EMP_INFO")
  @Entity
  public class EmployeeInfo {
      private Long id;
      private String email;
  
      @Column(name="EMPLOYEE_EMAIL")
      public String getEmail() {
          return email;
      }
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name = "emp_id", nullable = false)
      public Long getId() {
          return id;
      }
  }
  </code></pre>
  
  <p>The way I try to save it is...</p>
  
  <pre><code>Employee emp = new Employee();
  emp.setEnumId(SimpleEnum.COMPLETE);
  emp.setName("Shreyas");
  EmployeeInfo info = new EmployeeInfo();
  info.setEmail("Sh@gmail");
  concreteDAO.save(emp);   // This uses the JPATemplate provided by Spring JpaDaoSupport
  
  
  info.setId(emp.getId());
  concreteDAO.saveEmpInfo(info);
  </code></pre>
  
  <p>Any pointers would be much appreciated, as to how can I try to save the child entity?</p>