hibernate - Spring休眠@manyToMany与@jsonView无限递归的关系
问题描述
当我尝试序列化它们时,我遇到了 spring 和 jackson 的问题我有两个类
@Entity
@Table(name = "conditions")
@Data
@JsonInclude(Include.NON_EMPTY)
@NoArgsConstructor
public class Condition implements Serializable {
private static final long serialVersionUID = -2917417625227297889L;
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
@Column(name="nId")
@JsonView(View.Public.class)
private Integer id;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "conditions")
@JsonView(View.Public.class)
private Set<Rule> rules = new HashSet<Rule>(0);
public Condition(int conditionId) {
this.id = conditionId;
}
}
和
@Entity
@Table(name = "tblComplianceRules")
@Data
@NoArgsConstructor
public class Rule implements Serializable {
private static final long serialVersionUID = -4443637570696913241L;
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
@Column(nullable = false)
@JsonView(View.Public.class)
private Integer id;
@Column
@JsonView(View.Public.class)
@NotEmpty
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "tblComplianceRulesConditions", catalog = "swat", joinColumns = {
@JoinColumn(name = "ruleId", nullable = false, updatable = false) }, inverseJoinColumns = {
@JoinColumn(name = "conditionId", nullable = false, updatable = false) })
@JsonView(View.Public.class)
private Set<Condition> conditions = new HashSet<Condition>();
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "rules")
@JsonBackReference
@JsonView(View.Internal.class)
private Set<CompliancePolicy> policies = new HashSet<CompliancePolicy>(0);
public Rule(Integer id) {
this.id = id;
}
}
也生病发布 View 类
public class View {
public static class Public {
}
public static class Internal extends Public {
}
}
我试图实现的是,我发送给客户端的响应不会发送不必要的数据,但我的标题中不断出现错误......
我能够通过不使用@JsonView 并使用@JsonBackReference 来解决这个问题,但这不是我想要的......
根据要求,我附上了回复
@GetMapping(value = "/api/condition")
@JsonView(View.Internal.class)
public ResponseEntity<?> getConditions(Principal user) {
if (user != null) {
log.debug("Getting conditions for user: " + user.getName());
} else {
log.error("Warning user is not defined");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
try {
List<Condition> conditions = conditionService.getConditionsByOrderById();
return ResponseEntity.ok().body(conditions);
} catch (Exception ex) {
log.error("Could not fetch condition from tblComplianceConditions: " + ExceptionUtils.getRootCauseMessage(ex), ex);
return ResponseEntity.badRequest().body(ExceptionUtils.getRootCauseMessage(ex));
}
}
解决方案
我发现使用@JsonIgnore 来获取我需要的属性更容易我添加了一个@transient 字段并将其添加到那里。
推荐阅读
- r - 如何获得每个主成分解释的总方差
- powerbi - Power BI - 如何从原始源创建另一个表
- mvvm - Xamarin 在列表中形成列表(使用 prism 导航)
- python - pyodbc执行sqlserver存储过程——如何为用户定义表传入DEFAULT参数
- python - 如何在 HMTL/Bootstrap4 中使用变量作为选择器?
- c++ - 多线程中的工作池内存泄漏
- ios - 用原点阻止了一个框架
- java - 为什么下面代码的输出是这个映射:{1=[Dd,Ff], 2=[Dd,Ff], 3=[Dd,Ff], 4=[Dd,Ff]}?
- python - 如何将一个变量的输入变成一个列表?
- javascript - Revenuecat React-Native 产品获得空对象?