Spring Hibernate @manyToMany关系与@jsonView无限递归

时间:2019-08-22 11:41:56

标签: hibernate spring-boot jackson

当我尝试序列化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));
        }

    }

1 个答案:

答案 0 :(得分:0)

我发现使用@JsonIgnore更容易 对于我需要的属性,我添加了一个@transient字段并将其添加到那里。