I have a problem when creating a new record of the entity "Player".
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.
Player entity:
@Id @GeneratedValue private long ident;
@Column(length = 32, nullable = false) private String userName;
@Column(length = 128, nullable = false) private String fullName;
@Column(columnDefinition = "INT CHECK(self_evaluation BETWEEN 1 AND 10) NOT NULL") private int selfEvaluation;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = "Country.ident", nullable = false) Country country;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = "Occupation.ident", nullable = false) Occupation occupation;
Occupation entity:
@Id @GeneratedValue private long ident;
@Column(length = 32, nullable = false) private String occupation;
@OneToMany(mappedBy = "ident") private List<Player> player;
Country entity:
@Id @GeneratedValue private long ident;
@Column(length = 128, nullable = false) private String country;
@OneToMany(mappedBy = "ident") List<Player> player;
After entering all the parameters needed to create a player and selecting the country and occupation, the following output comes up.
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 (?, ?, ?, ?, ?, ?)