gpt4 book ai didi

java - Spring Boot ManyToMany 保留嵌套数据

转载 作者:行者123 更新时间:2023-12-02 09:08:57 26 4
gpt4 key购买 nike

我目前正在开发我的第一个 Spring boot 应用程序,我试图在两个实体之间保留多对多关系。目前,我在尝试将实体保存到我的端点时遇到错误。

用户实体

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(length = 16)
@ApiModelProperty(required = false, hidden = true)
private UUID uuid = UUID.randomUUID();

@Column(name = "name")
private String name;

@ManyToMany
@JoinTable(
name = "game_participants",
joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "game_id") }
)
private List<Game> games;
}

游戏实体

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "game")
public class Game {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(length = 16)
@ApiModelProperty(required = false, hidden = true)
private UUID uuid = UUID.randomUUID();

@Column(name = "name")
private String name;

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

我正在使用 OpenAPI(swagger),我正在尝试保存一个新的“游戏”实体。我的问题是,它期望获得如下结构的有效负载:

{
"name": "string",
"users": [
{
"games": [
null
],
"name": "string"
}
]
}

我真的不明白为什么它在我的用户实体中嵌套另一个游戏实体。我真正想要实现的是这样的:

{
"name": "string",
"users": [
{
"name": "string"
}
]
}

最佳答案

实现

{
"name": "string",
"users": [
{
"name": "string"
}
]
}

删除

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

来自游戏类。但是这样您就无法从游戏对象中引用用户。

关于java - Spring Boot ManyToMany 保留嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59565140/

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