title: Java - How to avoid duplicates when using @ManyToOne via REST
body: <p>I have a problem when creating a new record of the entity &quot;Player&quot;.</p>
  <p>When I create a new player I select his occupation and country from the lists. But the player record creates a duplicate of the selected country and occupation when I write it. I want it not to create a duplicate but to write to itself the ID of an already existing record from these lists.</p>
  <p>Player entity:</p>
  <pre><code>@Id @GeneratedValue private long ident;
  @Column(length = 32, nullable = false) private String userName;
  @Column(length = 128, nullable = false) private String fullName;
  @Column(columnDefinition = &quot;INT CHECK(self_evaluation BETWEEN 1 AND 10) NOT NULL&quot;) private int selfEvaluation;
  @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = &quot;Country.ident&quot;, nullable = false) Country country;
  @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = &quot;Occupation.ident&quot;, nullable = false) Occupation occupation;
  </code></pre>
  <p>Occupation entity:</p>
  <pre><code>@Id @GeneratedValue private long ident;
  @Column(length = 32, nullable = false) private String occupation;
  @OneToMany(mappedBy = &quot;ident&quot;) private List&lt;Player&gt; player;
  </code></pre>
  <p>Country entity:</p>
  <pre><code>@Id @GeneratedValue private long ident;
  @Column(length = 128, nullable = false) private String country;
  @OneToMany(mappedBy = &quot;ident&quot;) List&lt;Player&gt; player;
  </code></pre>
  <p>After entering all the parameters needed to create a player and selecting the country and occupation, the following output comes up.</p>
  <pre><code>Hibernate: select nextval ('hibernate_sequence')
  Hibernate: select nextval ('hibernate_sequence')
  Hibernate: select nextval ('hibernate_sequence')
  Hibernate: insert into country (country, ident) values (?, ?)
  Hibernate: insert into occupation (occupation, ident) values (?, ?)
  Hibernate: insert into player (country_ident, full_name, occupation_ident, self_evaluation, user_name, ident) values (?, ?, ?, ?, ?, ?)
  </code></pre>