title: Updating oneToMany entity sets parent to null
body: <p>I have a list of <code>Role</code> entities on a <code>User</code> entity linked with a <code>@OneToMany</code> relationship. When I change the list on the <code>User</code> entity, i.e change the roles, hibernate removes the old roles and adds the new ones as desired. However the foreign key field is set to null on the <code>Role</code> entity.</p>
  
  <p>Can you explian why the foregign key field is null, and how would I change it so it gets picked up on an update?</p>
  
  <p><strong>User.java</strong></p>
  
  <pre><code>@SerializedName("userrole")
  @Expose
  @OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
  private List&lt;Role&gt;   userRoles = new ArrayList&lt;Role&gt;();
  </code></pre>
  
  <p><strong>Role.java</strong></p>
  
  <pre><code>@ManyToOne
  @JoinColumn(name = "user")
  private User user;
  </code></pre>
  
  <p><strong>Table Before</strong></p>
  
  <pre><code>+----+------+------------------------+
  | id | role | user                   |
  +----+------+------------------------+
  |  1 |    0 | digby.tyson@gmail.com  |
  |  2 |    1 | digby.tyson@gmail.com  |
  |  3 |    2 | digby.tyson@gmail.com  |
  |  4 |    3 | digby.tyson@gmail.com  |
  |  5 |    4 | digby.tyson@gmail.com  |
  |  6 |    5 | digby.tyson@gmail.com  |
  |  7 |    6 | digby.tyson@gmail.com  |
  |  8 |    7 | digby.tyson@gmail.com  |
  |  9 |    5 | ronny.polley@gmail.com |
  | 10 |    6 | ronny.polley@gmail.com |
  | 11 |    7 | reed.robert@gmail.com  |
  +----+------+------------------------+
  </code></pre>
  
  <p><strong>Table After</strong></p>
  
  <pre><code>+----+------+------------------------+
  | id | role | user                   |
  +----+------+------------------------+
  |  9 |    5 | ronny.polley@gmail.com |
  | 10 |    6 | ronny.polley@gmail.com |
  | 11 |    7 | reed.robert@gmail.com  |
  | 12 |    0 | NULL                   |
  | 13 |    1 | NULL                   |
  | 14 |    2 | NULL                   |
  | 15 |    5 | NULL                   |
  | 16 |    6 | NULL                   |
  | 17 |    7 | NULL                   |
  +----+------+------------------------+
  </code></pre>
  
  <p><strong>Hibernate actions</strong></p>
  
  <pre><code>Hibernate: select userroles0_.user as user3_2_0_, userroles0_.id as id1_1_0_, userroles0_.id as id1_1_1_, userroles0_.role as role2_1_1_, userroles0_.user as user3_1_1_ from role userroles0_ where userroles0_.user=?
  Hibernate: insert into role (role, user) values (?, ?)
  Hibernate: insert into role (role, user) values (?, ?)
  Hibernate: insert into role (role, user) values (?, ?)
  Hibernate: insert into role (role, user) values (?, ?)
  Hibernate: insert into role (role, user) values (?, ?)
  Hibernate: insert into role (role, user) values (?, ?)
  Hibernate: update user set first_name=?, last_name=?, middle_name=?, organisation_id=? where email=?
  Hibernate: delete from role where id=?
  Hibernate: delete from role where id=?
  Hibernate: delete from role where id=?
  Hibernate: delete from role where id=?
  Hibernate: delete from role where id=?
  Hibernate: delete from role where id=?
  Hibernate: delete from role where id=?
  Hibernate: delete from role where id=?
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>The method that calls the database as requested in comments</p>
  
  <pre><code>@Override
  @Transactional
  public User update(User user) throws UserNotFoundException {
      String updatedUserEmail = user.getEmail();
  
      if (userRepository.findByEmail(updatedUserEmail) == null)
          throw new UserNotFoundException();
  
      return userRepository.save(user);
  
  }
  
  @Transactional
  public interface UserRepository extends CrudRepository&lt;User, Long&gt; {
  </code></pre>
  
  <p>Thanks for any help in advance.</p>