I have a Many-To-One relationship between two table in my database User table & Country table. The model classes are:
@Entity (name = "user")
public class User {
@Id @GeneratedValue (strategy = GenerationType.IDENTITY)
private int userId;
private String username;
private String password;
@ManyToOne (cascade = {CascadeType.ALL})
@JoinColumn (name = "countryId")
private Country country;
//Getter & Setter
}
@Entity (name = "country")
public class Country {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int countryId;
private String countryName;
//Getter & Setter
}
The problem I am having is when I save a new User object it also saves a new country record even when that country already exist in the database. How can I solve this problem?