title: Spring JPA/Hibernate @ManyToOne relationship always inserting child row
body: <p>I'm attempting to use Spring JPA/Hibernate to maintain a Parent table with a child table related based on an Id. If I attempt to insert the same object twice, the Parent table gets "updated" properly, but the child table is always inserted to even though the entry in the child table is already present.</p>
  
  <pre><code>CREATE TABLE `parent_table` (
    `parent_id` int(11) NOT NULL AUTO_INCREMENT,
    `ex_column_1` int(11) NOT NULL, // there is a unique constraint on this field - I just didn't include it
    `ex_column_2` int(11) NOT NULL,
    `child_id` int(11) NOT NULL,
    PRIMARY KEY (`parent_id`),
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p>Child Table:</p>
  
  <pre><code>CREATE TABLE `child_table` (
    `child_id` int(11) NOT NULL AUTO_INCREMENT,
    `child_col_1` varchar(200) DEFAULT NULL,
    PRIMARY KEY (`child_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p>POJOs:</p>
  
  <pre><code>@Entity
  @Table(name = "parent_table")
  public final class Parent {
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "parent_id")
      private long id;
  
      @Id
      @Column(name = "ex_column_1")
      private int exampleField;
  
      @Column(name = "ex_column_2")
      private int exampleField2;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "child_id", unique = true)
      private Child c;
  
      public Parent(/*params*/) {}
  
      // getters ...
  
      @Entity
      @Table(name = "child_table")
      public static final class Child {
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          @Column(name = "child_id")
          private long id;
  
          @Column(name = "child_col_1")
          private exampleChildField;
  
          public Child(/*params*/) {}
  
          // getters ...
      }
  }
  </code></pre>
  
  <p>Actual example of how POJO's are constructed and saved:</p>
  
  <pre><code>@RestController
  @RequestMapping("/parents")
  public final class ParentController {
      private final ParentRepository parentRepository;
  
      @Inject
      public ParentController(final ParentRepository parentRepository) {
          parentRepository = parentRepository;
      }
  
      @RequestMapping(value = "", method=RequestMethod.PUT)
      public void updateParents(@RequestBody Parent[] parents) {
  
          // ignore input for now, test by constructing object
  
          Parent.Child c = new Parent.Child(0L, "test 1");
          Parent p = new Parent(0L, "unique_column_1", "column_2", c);
          Set&lt;Parent&gt; pSet = new HashSet&lt;&gt;();
          pSet.add(p);
  
         parentRepository.save(pSet);
      }
  }
  </code></pre>
  
  <p>Repository Layer:</p>
  
  <pre><code>public interface ParentRepository extends CrudRepository&lt;Parent, Long&gt;, ParentRepositoryCustom {}
  
  public interface ParentRepositoryCustom {
      void save(Set&lt;Parent&gt; parents);    
  }
  
  @Repository
  final class ParentRepositoryImpl implements ParentRepositoryCustom {
      private final EntityManager entityManager;
  
      @Inject
      public EmployerRepositoryImpl(final EntityManager entityManager) {
          this.entityManager = entityManager;
      }
  
      @Override
      @Transactional
      public void save(Set&lt;Parent&gt; parents) {
          parents.forEach(parent -&gt; {
              Session session = (Session) entityManager.getDelegate();
              session.saveOrUpdate(parent);
          });
      }
  }
  </code></pre>
  
  <p>If the entry in either table doesn't exist, it persists both entities just fine and links the proper Ids between tables. The issue occurs if the same parent and child object are inserted again. The UPDATE occurs on the Parent table, but an INSERT on the child:</p>
  
  <pre><code>Hibernate: insert into child_table (child_col_1) values (?)
  Hibernate: insert into parent_table (ex_column_1, ex_column_2, child_id) values (?, ?, ?)
  </code></pre>
  
  <p>On second insert:</p>
  
  <pre><code>Hibernate: insert into child_table (child_col_1) values (?)
  Hibernate: update parent_table set ex_column_2=?, child_id=? where ex_column_1=? 
  </code></pre>
  
  <p>How do I get EntityManager to persist these correctly if the entry already exists?</p>