为什么会出现唯一索引或主键违规?
Why am I getting a Unique index or primary key violation?
我正在尝试使用 Spring MVC 在两个人之间添加友谊关系。
第一次调用顺利,但第二次调用抛出唯一索引或主键冲突?,为什么我得到它?
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@ManyToMany(fetch = FetchType.EAGER)
@ElementCollection
private List<Person> friends;
@ManyToMany(mappedBy="friends", fetch = FetchType.EAGER)
@ElementCollection
private List<Person> friendOf;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Person> getFriends() {
return friends;
}
public void setFriends(List<Person> friends) {
this.friends = friends;
}
public List<Person> getFriendOf() {
return friendOf;
}
public void setFriendOf(List<Person> friendOf) {
this.friendOf = friendOf;
}
public void addFriend(Person person){
if(this.getFriends() == null){
this.friends = new ArrayList<>();
}
this.friends.add(person);
}
public void setFriendship(Long firstPersonId, Long scndPersonId){
Person firstPerson = personService.getPerson(firstPersonId);
firstPerson.addFriend(personService.getPerson(scndPersonId));
personService.savePerson(firstPerson);
Person scndPerson = personService.getPerson(scndPersonId);
scndPerson.addFriend(personService.getPerson(firstPersonId));
personService.savePerson(scndPerson);
}
Person pup1 = new Person();
Long pupId1 = controller.savePerson(pup1);
Person pup2 = new Person();
long pupId2 = controller.savePerson(pup2);
setFriendship(pupId1, pupId2);
Person pup3 = new Person();
long pupId3 = controller.savePerson(pup3)
controller.setFriendship(pupId3, pupId1); //This throws an exception
controller.setFriendship(pupId3, pupId2);
为什么标记的行会导致异常?第一次调用 p1 和 p2 之间的 setFrienship 成功,但是当我尝试在 p1 和 p3 之间建立连接时失败,出现以下异常:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PUBLIC.UK_6VEJRSUXRONFC95I7O5XJNRMU_INDEX_D ON PUBLIC.PERSON_FRIENDS(FRIENDS_ID) VALUES 2
您需要直接指定连接和反向连接列。请注意,它们在集合中交换。
@ManyToMany
@JoinTable(name="friends",
joinColumns=@JoinColumn(name="personId"),
inverseJoinColumns=@JoinColumn(name="friendId")
)
private List<User> friends;
@ManyToMany
@JoinTable(name="friends",
joinColumns=@JoinColumn(name="friendId"),
inverseJoinColumns=@JoinColumn(name="personId")
)
private List<User> friendOf;
请看一下这个答案,希望你能答对。您需要做的更多是指定连接列
Hibernate recursive many-to-many association with the same entity
您也有可能尝试将同一个实体持久化两次,在您的例子中是 pub1 。正在尝试使用合并!
你的 setFriendship 方法调用你的服务来保存你已经做过一次的实体,这就是它给你唯一键冲突的原因,因为实体已经存在。
如果您需要更多解释,请发表评论!
我正在尝试使用 Spring MVC 在两个人之间添加友谊关系。 第一次调用顺利,但第二次调用抛出唯一索引或主键冲突?,为什么我得到它?
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@ManyToMany(fetch = FetchType.EAGER)
@ElementCollection
private List<Person> friends;
@ManyToMany(mappedBy="friends", fetch = FetchType.EAGER)
@ElementCollection
private List<Person> friendOf;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Person> getFriends() {
return friends;
}
public void setFriends(List<Person> friends) {
this.friends = friends;
}
public List<Person> getFriendOf() {
return friendOf;
}
public void setFriendOf(List<Person> friendOf) {
this.friendOf = friendOf;
}
public void addFriend(Person person){
if(this.getFriends() == null){
this.friends = new ArrayList<>();
}
this.friends.add(person);
}
public void setFriendship(Long firstPersonId, Long scndPersonId){
Person firstPerson = personService.getPerson(firstPersonId);
firstPerson.addFriend(personService.getPerson(scndPersonId));
personService.savePerson(firstPerson);
Person scndPerson = personService.getPerson(scndPersonId);
scndPerson.addFriend(personService.getPerson(firstPersonId));
personService.savePerson(scndPerson);
}
Person pup1 = new Person();
Long pupId1 = controller.savePerson(pup1);
Person pup2 = new Person();
long pupId2 = controller.savePerson(pup2);
setFriendship(pupId1, pupId2);
Person pup3 = new Person();
long pupId3 = controller.savePerson(pup3)
controller.setFriendship(pupId3, pupId1); //This throws an exception
controller.setFriendship(pupId3, pupId2);
为什么标记的行会导致异常?第一次调用 p1 和 p2 之间的 setFrienship 成功,但是当我尝试在 p1 和 p3 之间建立连接时失败,出现以下异常:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PUBLIC.UK_6VEJRSUXRONFC95I7O5XJNRMU_INDEX_D ON PUBLIC.PERSON_FRIENDS(FRIENDS_ID) VALUES 2
您需要直接指定连接和反向连接列。请注意,它们在集合中交换。
@ManyToMany
@JoinTable(name="friends",
joinColumns=@JoinColumn(name="personId"),
inverseJoinColumns=@JoinColumn(name="friendId")
)
private List<User> friends;
@ManyToMany
@JoinTable(name="friends",
joinColumns=@JoinColumn(name="friendId"),
inverseJoinColumns=@JoinColumn(name="personId")
)
private List<User> friendOf;
请看一下这个答案,希望你能答对。您需要做的更多是指定连接列
Hibernate recursive many-to-many association with the same entity
您也有可能尝试将同一个实体持久化两次,在您的例子中是 pub1 。正在尝试使用合并!
你的 setFriendship 方法调用你的服务来保存你已经做过一次的实体,这就是它给你唯一键冲突的原因,因为实体已经存在。
如果您需要更多解释,请发表评论!