Spring ManyToMany删除结果导致ConstraintViolationException

时间:2018-12-01 18:48:57

标签: spring hibernate jpa

我正在尝试使用Rest调用删除Competition实体,但是却收到内部服务器错误消息,可能是因为我与@ManyToMany实体具有User关系。

error: "Internal Server Error"
message: "could not execute statement; SQL [n/a]; constraint ["FK2YX8G5HN2Q4HFX6MI59O1FO0Y: PUBLIC.MATCHES FOREIGN KEY(COMPETITION_ID) REFERENCES PUBLIC.COMPETITIONS(ID) (1)"; SQL statement:↵delete from competitions where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
path: "/competitions/1"
status: 500
timestamp: "2018-12-01T18:38:36.725+0000"

这是我的用户实体

@ManyToMany(fetch = FetchType.LAZY, targetEntity=Competition.class, cascade =
            {
                    CascadeType.DETACH,
                    CascadeType.MERGE,
                    CascadeType.PERSIST
            })
    @JoinTable(name = "competitions_officials",
            joinColumns = {@JoinColumn(name ="official_id")},
            inverseJoinColumns = {@JoinColumn(name = "competition_id")})
    @JsonIgnore
    private List<Competition> competitions;

这是我的比赛实体

@ManyToMany(fetch = FetchType.LAZY, targetEntity=User.class, cascade =
            {
                    CascadeType.DETACH,
                    CascadeType.MERGE,
                    CascadeType.PERSIST
            })
    @JoinTable(name = "competitions_officials",
            joinColumns = {@JoinColumn(name ="competition_id")},
            inverseJoinColumns = {@JoinColumn(name = "official_id")})
    private List<User> officials;

这是我的CompetitionController

@DeleteMapping("/{id}/officials")
    public ResponseEntity<User> deleteOfficials(
            @PathVariable Long id) {
        Optional<Competition> oCompetitions = getEntity(id);
        if(oCompetitions.isPresent()){
            for (Iterator<User> userIterator = userRepository.findByCompetitionsId(id).iterator(); userIterator.hasNext();) {
                User user = userIterator.next();
                oCompetitions.get().getOfficials().remove(user);
            }
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.notFound().build();
    }

尽管我不确定这是否是问题的一部分,但是这是我的sql插入内容:

INSERT INTO competitions_officials (competition_id, official_id) VALUES (1, 2);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (1, 3);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (2, 4);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (2, 5);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (3, 6);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (3, 2);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (3, 3);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (4, 6);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (5, 2);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (5, 4);
INSERT INTO competitions_officials (competition_id, official_id) VALUES (5, 6);

GET的请求工作正常,并且添加了新的Competition。仅删除是有问题的。

我已经从网站上尝试了许多答案,但是仍然出现此错误。感谢您的提前帮助。

0 个答案:

没有答案