如何解决Spring Boot和MySQL获得一对多关系的请求
我在MySQL,用户,机构和角色中有3个表
- 用户与机构之间的一对多关系(一个用户可以在一个机构工作,而一个机构可以有很多用户)
数据库中的机构已经插入,用户只能在特定的现有机构中工作。
我将2条记录手动添加到我的数据库中,供用户和机构使用。
但是当我发送getAllUsers或getAllInstitutions请求时,会得到以下json主体,这是一个无限循环。
---------用户模型--------
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="user_Id")
private int userId;
@Column(name="name")
private String name;
@Column(name="lastname")
private String lastname;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
@Column(name="isActive")
private boolean isActive;
@Column(name="lastActive")
private String lastActive;
@Column(name="createdDate")
private String createdDate;
@Column(name="isBlocked")
private boolean isBlocked;
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "institution_id",nullable = false)
private Institution institution;
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST)
@JoinTable(name = "user_has_role",joinColumns = {
@JoinColumn(name = "user_id",referencedColumnName = "user_id",nullable = false,updatable = true)},inverseJoinColumns = {
@JoinColumn(name = "role_id",referencedColumnName = "role_id",updatable = true)})
private Set<Role> roles = new HashSet<>();
}
---------机构模型-----------
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "institution")
public class Institution {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="institution_Id")
private int institutionId;
@Column(name="name")
private String name;
@Column(name="type")
private String type;
@Column(name="location")
private String location;
@OneToMany(mappedBy = "institution",fetch = FetchType.LAZY)
private Set<User> user;
}
---------角色模型----------
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="role_Id")
private int roleId;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
@ManyToMany(mappedBy = "roles",fetch = FetchType.LAZY)
private Set<User> users = new HashSet<>();
}
--------控制器-----------
@GetMapping("/getAllUsers")
public List<User> getAllUsers() {
return (List<User>) userrepository.findAll();
}
@PostMapping("/addUser")
public String addUser(@RequestBody User user) {
userrepository.save(user);
return "role saved with name: " + user.getName();
}
@GetMapping("/getAllInstitutions")
public List<Institution> getAllInstitutions() {
return (List<Institution>) institutionrepository.findAll();
}
@PostMapping("/addInstitution")
public String addInstitution(@RequestBody Institution institution) {
institutionrepository.save(institution);
return "institution saved with name: " + institution.getName();
}
--------- getAllInstitutions的api请求主体循环错误---------------
[
{
"institutionId": 1,"name": "Bank Byblos","type": "Bank","location": "sen el fil","user": [
{
"userId": 3,"name": "user1","lastname": "testtest","email": "user1@hotmail.com","password": "user1123","lastActive": "11/11/21","createdDate": "06/05/20","institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
---------getAllÙsers的api请求主体循环错误---------------
[
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
{
"userId": 3,"institution": {
"institutionId": 1,"user": [
我有2个用户添加到我的数据库user1和user2中,并且还有2个添加到我的数据库user1的机构属于一个,而user2属于另一个(使用user表中的外键stitution_id) 请你能帮我解决这个问题吗?
解决方法
尝试如下修改您的代码:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "institution")
public class Institution {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="institution_Id")
private int institutionId;
@Column(name="name")
private String name;
@Column(name="type")
private String type;
@Column(name="location")
private String location;
@OneToMany(mappedBy = "institution",fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = {"institution"})
private Set<User> user;
}
使用@JsonIgnoreProperties
批注忽略Institution
模型中用户的属性指令。
现在它不会循环,因为在获取机构数据时您不会有任何想法。
也尝试在@JsonIgnoreProperties
模型中添加User
,这样可以解决您的问题。
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="user_Id")
private int userId;
@Column(name="name")
private String name;
@Column(name="lastname")
private String lastname;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
@Column(name="isActive")
private boolean isActive;
@Column(name="lastActive")
private String lastActive;
@Column(name="createdDate")
private String createdDate;
@Column(name="isBlocked")
private boolean isBlocked;
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "institution_id",nullable = false)
@JsonIgnoreProperties(value = {"user"})
private Institution institution;
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。