gpt4 book ai didi

java - Hibernate 在尝试使用合并方法更新对象时抛出 DataIntegrityViolationException

转载 作者:行者123 更新时间:2023-11-29 02:57:48 28 4
gpt4 key购买 nike

以前的 update() 方法抛出 具有相同标识符值的不同对象已经与 session 关联 所以我将 dao 更改为 merge .现在它给出 org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [不适用];约束[空];嵌套异常是 org.hibernate.exception.ConstraintViolationException: 无法执行语句

TeamDAOImpl

public void updateTeam(Team team) {
Team teamToUpdate = getTeam(team.getId());
teamToUpdate.setName(team.getName());
teamToUpdate.setRating(team.getRating());
Set<TeamMember> teamMember = team.getTeamMembers();
teamToUpdate.setTeamMembers(teamMember);
getCurrentSession().merge(teamToUpdate);
}

团队实体

@Entity
@Table(name="teams")
public class Team {


private Integer id;

private String name;

private Integer rating;

private Set<TeamMember> teamMembers;

// ...
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "team_member", joinColumns =
@JoinColumn(name = "FK_Team_id", referencedColumnName= "id"),
inverseJoinColumns = @JoinColumn(name = "FK_Member_id", referencedColumnName = "id")
)
public Set<TeamMember> getTeamMembers() {
return teamMembers;
}

public void setTeamMembers(Set<TeamMember> teamMembers) {
this.teamMembers = teamMembers;
}
}

Team 与团队成员建立多对多关系。在 SQL 级别,它是使用表完成的:teamsmemberteam_member

违反了哪些约束?如何解决?

更新:

CREATE TABLE `member` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `teams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`rating` int(11) NOT NULL,
`FK_Organization_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_Organization_id` (`FK_Organization_id`),
CONSTRAINT `FK_Organization_id` FOREIGN KEY (`FK_Organization_id`) REFERENCES `organization` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `team_member` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`FK_Member_id` int(11) NOT NULL,
`FK_Team_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_Member_id` FOREIGN KEY (`id`) REFERENCES `member` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_Team_id` FOREIGN KEY (`id`) REFERENCES `teams` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Cannot add or update a child row: a foreign key constraint fails (db.team_member, CONSTRAINT FK_Member_id FOREIGN KEY (id) REFERENCES member (id) ON DELETE NO ACTION ON UPDATE NO ACTION)

数据:

enter image description here

更新 3

public Team getTeam(int id) {
Team team = (Team) getCurrentSession().get(Team.class, id);
return team;
}

更新4P6Spy 帮助发现错误查询:

insert into team_member (FK_Team_id, FK_Member_id) values (2, 2);

返回:

[SQL] insert into team_member (FK_Team_id, FK_Member_id) values (2, 2);
[Err] 1452 - Cannot add or update a child row: a foreign key constraint fails (`db`.`team_member`, CONSTRAINT `FK_Member_id` FOREIGN KEY (`id`) REFERENCES `member` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

最佳答案

问题出在 team_member 表中的 CONSTRAINT 定义中。而不是:

CONSTRAINT `FK_Member_id` FOREIGN KEY (`id`) REFERENCES `member` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_Team_id` FOREIGN KEY (`id`) REFERENCES `teams` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION

必须是

CONSTRAINT `FK_Member_id` FOREIGN KEY (`FK_Member_id`) REFERENCES `member` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_Team_id` FOREIGN KEY (`FK_Team_id`) REFERENCES `teams` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION

关于java - Hibernate 在尝试使用合并方法更新对象时抛出 DataIntegrityViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28634138/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com