带引用列的Hibernate条件返回无限错误

时间:2017-03-29 13:34:49

标签: java spring hibernate projection

我有一个表格的模型

@Entity(name="gpl_bd_stage")
public class GplBdStage implements Serializable {

/** Primary key. */
protected static final String PK = "stageId";

/**
 * The optimistic lock. Available via standard bean get/set operations.
 */
@Column(name="created_on")
private Timestamp createdOn;
@Column(nullable=false, length=250)
private String stage;
@Column(name="updated_on")
private Timestamp updatedOn;
@Column(name="record_display_order", precision=2)
private BigDecimal recordDisplayOrder;
@Column(name="escalation_1_timeline", precision=131089)
private BigDecimal escalation1Timeline;
@Column(name="escalation_2_timeline", precision=131089)
private BigDecimal escalation2Timeline;
@Column(name="escalation_3_timeline", precision=131089)
private BigDecimal escalation3Timeline;
@Id @Column(name="stage_id", unique=true, nullable=false, precision=10)
private int stageId;
@Column(name="tat_days", precision=131089)
private BigDecimal tatDays;
@Column(name="is_active", length=1)
private boolean isActive;
@OneToMany(mappedBy="gplBdStage")
private Set<GplBdBusinessDeal> gplBdBusinessDeal;
@OneToMany(mappedBy="gplBdStage")
private Set<GplBdDealEscalation> gplBdDealEscalation;
@OneToMany(mappedBy="gplBdStage")
private Set<GplBdEscalationRule> gplBdEscalationRule;
@ManyToOne @JoinColumn(name="created_by")
private GplBdUser gplBdUser;

@OneToMany(mappedBy="gplBdStage3")

private Set<GplBdStage> gplBdStage4;

@ManyToOne @JoinColumn(name="parent_stage")
private GplBdStage gplBdStage3;
@ManyToOne @JoinColumn(name="updated_by")
private GplBdUser gplBdUser2;
@OneToMany(mappedBy="gplBdStage")
private Set<GplBdTag> gplBdTag;

当我尝试使用以下查询从表中获取特定列时:

Criteria cr = session.createCriteria(GplBdStage.class,"e")
        .setProjection(Projections.projectionList()
        .add(Projections.property("gplBdStage3"),"gplBdStage3")
        .add(Projections.property("stageId"),"stageId")
        .add(Projections.property("stage"),"stage"))
        .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<GplBdStage> result = cr.list();

它提供无限递归和无限结果

我想要这样的东西

[{"stage":"First Contract","gplBdStage3":{"stage":"First Contract","stageId":1},"stageId":1}]

我使用spring boot hibernate

1 个答案:

答案 0 :(得分:0)

我认为你得到了无限的json循环,因为你有@OneToMany@ManyToOne个关联。

尝试使用@JsonManagedReference@JsonBackReference注释

来限制它

为了让Jackson工作得很好,关系的两个方面之一不应该被序列化,以避免导致你的stackoverflow错误的infite循环。

    // This is pseudo code
    @OneToMany(mappedBy = "--" cascade = CascadeType.ALL)
    @JsonManagedReference
    private Set<?> -- = ;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "--", nullable = false)
    @JsonBackReference
    private -- --;

如果对获取某些实体数据不感兴趣,只需在任何一个类

中使用@JsonIgnore