在springBoot数据jpa错误404中对具有manyToMany关系的实体的表发布请求

zsbz8rwp  于 2022-12-02  发布在  Spring
关注(0)|答案(1)|浏览(184)

我试图向我的用户表发出post请求,用户实体与竞赛实体有多对多关系,竞赛实体与团队实体有多对多关系,但是我收到错误404。代码如下:
用户名:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String email;

    private String password;

    private Boolean isAdmin;

    @ManyToMany
    private List<Contest> contests;

}

竞赛内容:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "contest")
public class Contest {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    @ManyToMany(mappedBy = "contests")
    private List<Team> teams;

    @ManyToMany(mappedBy = "contests")
    private List<User> users;

}

团队:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "team")
public class Team {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToMany
    private List<Contest> contests;

    private String name;

    private int wins, looses;

}

发布请求JSON:

{
    "email":"john",
    "password":"bravo",
    "isAdmin":true

}

我似乎不能使它工作,在创建关系之前,其余的API工作正常,所以问题不在我的仓库,服务,也不是控制器。
我试着在Postman中使用JSON来创建post请求,但是我得到了错误404,我希望能够在我的表中保存新的实体。

k4emjkb1

k4emjkb11#

你应该尝试用@JoinTable,因为@ManyToMany关系需要更多一点的Map,请参考本指南,它应该会有所帮助。
https://www.baeldung.com/jpa-many-to-many

相关问题