I am trying to map the following:
public class Person{
...
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "person_categories", joinColumns = @JoinColumn(name = "personId"), inverseJoinColumns = @JoinColumn(name = "categoryId"))
private Set<Category> categories
...
}
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "categoryId")
private Integer id;
@Column(unique = true)
private Sting name
...
}
When I attempt to save a Person object, the categories are inserted, but duplicates are stored into the table. How can I ensure each category is unique in its table and while maintaining transitive persistence? I have tried making the name column unique, but that only helped by throwing constraint errors.