响应中未显示复合对象 - Spring 引导
Composite object not showing up in the response - Spring Boot
所以我在我的系统中引入了一些角色。我有一个看起来像这样的 class :
@Entity
@Table(name = "user_role_mapping")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class UserRoleMapping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
Integer id;
@Column(name = "role_id")
Role role;
@ManyToOne
@JoinColumn(name = "user_id")
User user;
public UserRoleMapping(Role role, User user) {
this.role = role;
this.user = user;
}
}
由此映射的 classes 如下所示:
public enum Role {
ROLE_CUSTOMER(1) , ROLE_ADMIN(0);
public int id;
private Role (int id){
}
}
用户 class 看起来像:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "phone")
private String phoneNumber;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles;
}
对于初始 POC,我编写了一个简单的 API 方法,如下所示:
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<?> returnUserAndDefaultRole( @RequestParam(name = "phone") String phone) {
User user = usersRepository.findByPhoneNumber(phone);
List<UserRoleMapping> urm = userRoleMappingRepository.findAllByUser(user);
UserRoleMapping userRoleMapping;
if (urm.size() == 0) {
userRoleMapping = new UserRoleMapping(Role.ROLE_CUSTOMER, user);
userRoleMappingRepository.save(userRoleMapping); }
else {
userRoleMapping = urm.get(0);
}
Map<String, Object> result = new HashMap<>();
result.put("User", user );
result.put("Role_info" ,userRoleMapping);
return new ResponseEntity<>(result, HttpStatus.OK);
}
在这里,我希望得到一个 User 对象,它也有一个 Set 以及一个在 Role_info
键下的 userRoleMapping 对象。
我的实际输出如下:
"User": {
"id": 1542275,
"phoneNumber": "9527725710",
"name": "njari",
"userRoles": []
},
"Role_info": {
"id": 1,
"role": "ROLE_CUSTOMER",
"user": {
"id": 1542275,
"phoneNumber": "9527725710",
"name": "njari",
"userRoles": []
}
}
}
我的 Role_Info 已填充。但是,用户对象中的 userRoles 不是。
首先,我想要填充内部 Set of userRoles。
其次,我也不希望 Role_Info 包含整个 User 对象,我该如何实现?
如果我没看错,请尝试更改您的映射
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles;
收件人:
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles;
要排除 field/object,我正在使用 @JsonIgnore:
@JsonIgnore
@ManyToOne
@JoinColumn(name = "user_id")
User user;
首先,我建议在带有@Transnational 注释的服务方法中使用存储库,而不是在控制器中。
其次,您需要将新对象UserRoleMapping 添加到User userRoles 并保存User (cascade = CascadeType.ALL)。我建议更改
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles;
至
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles = new HashSet<>();
所以我在我的系统中引入了一些角色。我有一个看起来像这样的 class :
@Entity
@Table(name = "user_role_mapping")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class UserRoleMapping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
Integer id;
@Column(name = "role_id")
Role role;
@ManyToOne
@JoinColumn(name = "user_id")
User user;
public UserRoleMapping(Role role, User user) {
this.role = role;
this.user = user;
}
}
由此映射的 classes 如下所示:
public enum Role {
ROLE_CUSTOMER(1) , ROLE_ADMIN(0);
public int id;
private Role (int id){
}
}
用户 class 看起来像:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "phone")
private String phoneNumber;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles;
}
对于初始 POC,我编写了一个简单的 API 方法,如下所示:
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<?> returnUserAndDefaultRole( @RequestParam(name = "phone") String phone) {
User user = usersRepository.findByPhoneNumber(phone);
List<UserRoleMapping> urm = userRoleMappingRepository.findAllByUser(user);
UserRoleMapping userRoleMapping;
if (urm.size() == 0) {
userRoleMapping = new UserRoleMapping(Role.ROLE_CUSTOMER, user);
userRoleMappingRepository.save(userRoleMapping); }
else {
userRoleMapping = urm.get(0);
}
Map<String, Object> result = new HashMap<>();
result.put("User", user );
result.put("Role_info" ,userRoleMapping);
return new ResponseEntity<>(result, HttpStatus.OK);
}
在这里,我希望得到一个 User 对象,它也有一个 Set 以及一个在 Role_info
键下的 userRoleMapping 对象。
我的实际输出如下:
"User": {
"id": 1542275,
"phoneNumber": "9527725710",
"name": "njari",
"userRoles": []
},
"Role_info": {
"id": 1,
"role": "ROLE_CUSTOMER",
"user": {
"id": 1542275,
"phoneNumber": "9527725710",
"name": "njari",
"userRoles": []
}
}
}
我的 Role_Info 已填充。但是,用户对象中的 userRoles 不是。 首先,我想要填充内部 Set of userRoles。 其次,我也不希望 Role_Info 包含整个 User 对象,我该如何实现?
如果我没看错,请尝试更改您的映射
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER, cascade = CascadeType.ALL) private Set<UserRoleMapping> userRoles;
收件人:
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles;
要排除 field/object,我正在使用 @JsonIgnore:
@JsonIgnore @ManyToOne @JoinColumn(name = "user_id") User user;
首先,我建议在带有@Transnational 注释的服务方法中使用存储库,而不是在控制器中。
其次,您需要将新对象UserRoleMapping 添加到User userRoles 并保存User (cascade = CascadeType.ALL)。我建议更改
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles;
至
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<UserRoleMapping> userRoles = new HashSet<>();