title: Not-null property references a null or transient value for persisted value
body: <p>I'm trying to persist two different entities using JPA1, with the Hibernate implementation.
  The code for this is as shown below:</p>
  
  <p>Parent entity class</p>
  
  <pre><code>@Entity
  @Table(name = "parent")
  public class Parent implements Serializable {
  
      {...}
      private Child child;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "child_id", nullable = "false")
      public Child getChild() {
          return child;
      }
  
      public void setChild(Child child) {
          this.child = child;
  }
  </code></pre>
  
  <p>Child entity class </p>
  
  <pre><code>@Entity
  @Table(name = "child")
  public class Child implements Serializable {
  
      private Integer id;
  
      @Id
      @Column(name = "child_id")
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>Test case</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:META-INF/application.xml")
  @Transactional
  public class ParentTest extends TestCase {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Test
      public void testSave() {
          Child child = new Child();
          child.setId(1);
  
          Parent parent = new Parent();
          parent.setChild(child);
  
          entityManager.persist(parent.getChild());
          entityManager.persist(parent); // throws the exception
      }
  }
  </code></pre>
  
  <p>Entity manager and transaction on application.xml</p>
  
  <pre><code>&lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; 
  
  &lt;jee:jndi-lookup id="dataSource" jndi-name="java:/jdbc/myds" expected-type="javax.sql.DataSource" /&gt; 
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="packagesToScan" value="com.mypackage" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt; 
      &lt;property name="jpaVendorAdapter"› 
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
      &lt;/property&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect&gt;org.hibernate.dialect.Oracle10gDialect&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt; 
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; 
  &lt;/bean&gt;
  </code></pre>
  
  <p>When trying to insert the parent object, hibernate throws a PropertyValueException, saying child is null or transient, even though child was created and persisted before this operation. The odd thing is this only fails on the unit test, and in the real application, with a pre-inserted child, this works just as expected.</p>
  
  <p>PS:
  I'm pretty aware I could map child with cascade persist, but that's not the idea here. I just want to check if these two work independently.</p>