gpt4 book ai didi

java - 当数据库中有多个对象时,Spring boot rest 响应在 OneToMany 上给出 1 个结果

转载 作者:行者123 更新时间:2023-11-29 07:20:51 24 4
gpt4 key购买 nike

对于一个学校项目,我们必须制作一个与我们的 MySQL 数据库通信的 spring boot rest 服务器。这是数据库的屏幕截图:

enter image description here

对于我的用户模型,我有以下内容:

@Entity
@Table(name = "User")
@Setter
public class User extends ResourceSupport implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_user", updatable = false, nullable = false)
@JsonSerialize
private Long id;
@Getter
private String firstName;
@Getter
private String lastName;
@Getter
private String email;
@Getter
private String password;
@Getter
private Double hourlyWage;
@Getter
private String adress;
@Getter
private String city;
@Getter
private Boolean isManager;
@Getter
@OneToMany
@JoinTable(name = "user_to_project", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn(name = "project_id"))
private List<Project> projects;

然后对于我的项目类:

@Entity
@Table(name = "project")
@Setter
public class Project extends ResourceSupport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_project")
@JsonSerialize
private Long id;
@Getter
@OneToOne
@JoinColumn(name = "owner", referencedColumnName = "id")
private Company owner;
@Getter
private String description;
@Getter
private String name;
@Getter
private double payout;
@Getter
private boolean internal;
@Getter
@OneToMany
@JoinTable(name = "user_to_project", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
private List<User> users;

我不知道我做错了什么,因为这是我得到的结果:

enter image description here

当我的链接表中有这个时:

enter image description here

我希望有人能回答我的问题,因为我已经厌倦了 spring boot 和这个学校项目。

最佳答案

我不知道它是否对你有帮助,但下面是我的代码:

// Role
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToMany(mappedBy = "roles")
@JsonIgnoreProperties("roles")
@OrderBy("id")
private List<User> users;

// User
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToMany
@JoinTable(name = "users_roles",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
@JsonIgnoreProperties("users")
@OrderBy("id")
private List<Role> roles;

另外,请注意 - 在 JPA 中,一个表是“主”(在我的示例中是用户),另一个是“从”,而不是 m2m 表相等的 SQL。 "Slave"应该在 @ManyToMany 注释中有修饰符 mappedBy

表 DDL(postgres):

create table roles
(
id serial not null
constraint roles_pkey
primary key,
version bigint default 0
);

create table users
(
id serial not null
constraint users_pkey
primary key,
version bigint default 0
);

create table users_roles
(
user_id integer not null
constraint fk_users
references users,
role_id integer not null
constraint fk_roles
references roles,
constraint users_roles_pkey
primary key (user_id, role_id)
);

希望对您有所帮助。祝你好运。

关于java - 当数据库中有多个对象时,Spring boot rest 响应在 OneToMany 上给出 1 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56384041/

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