title: Spring Data JPA ManyToOne and OneToMany - Unable to find entity with ID
body: <p>I have three tables - <code>role</code>, <code>user</code>, and <code>user_role</code>. This is supposed to be <code>ManyToMany</code> but since I also want to generate id for the <code>user_role</code>, I used <code>OneToMany</code> and <code>ManyToOne</code>.</p>
  <p>Here are my entities with relevant fields only:</p>
  <pre><code>@Entity
  public class Role {
      @Id
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      private Integer id;
      @OneToMany(fetch = FetchType.EAGER, mappedBy = &quot;role&quot;)
      private Set&lt;UserRole&gt; userRoles;
  }
  
  @Entity
  public class User {
      @Id
      private String id;
      @OneToMany(fetch = FetchType.EAGER, mappedBy = &quot;user&quot;)
      private Set&lt;UserRole&gt; userRoles;
  }
  
  @Entity
  public class UserRole {
      @Id
      private String id;
      @ManyToOne
      @JoinColumn(name = &quot;user_id&quot;)
      private User user;
      @ManyToOne
      @JoinColumn(name = &quot;role_id&quot;)
      private Role role;  
  }
  </code></pre>
  <p>Then, this is how I create instances of them and save to the DB:</p>
  <pre><code>// Retrieve from DB by ID
  Role role = ...;
  
  // ID String is generated from UUID
  User user = new User();
  user.id(&quot;abc&quot;);
  
  // ID String is generated from UUID
  UserRole userRole = new UserRole();
  userRole.setId(&quot;xyz&quot;);
  
  Set&lt;UserRole&gt; userRoles = Set.of(userRole);
  
  role.setUserRoles(userRoles);
  user.setUserRoles(userRoles);
  
  userRole.setUser(user);
  userRole.setRole(role);
  
  userRepository.save(user);
  </code></pre>
  <p>The issue that I find it difficult to resolve no matter how I have tried and googled:</p>
  <pre><code>2020-09-27 23:41:58.917  WARN 21948 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.example.entity.UserRole with id xyz; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.example.entity.UserRole with id xyz]
  </code></pre>
  <p>Please help me. Thanks.</p>