【问题标题】:Hibernate criteria with referenced column returning infinite error带有引用列的休眠条件返回无限错误
【发布时间】:2017-03-29 13:34:48
【问题描述】:

我有一张桌子的模型

@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休眠

【问题讨论】:

    标签: java spring hibernate projection


    【解决方案1】:

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

    尝试使用@JsonManagedReference@JsonBackReference 注释来限制它

    为了让 Jackson 正常工作,关系的两侧之一不应序列化,以避免导致您的 stackoverflow 错误的无限循环。

        // 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

    【讨论】:

    • 但是现在我无法从 repo 中的 finall 访问该库
    • 什么是finall和repo?
    • 你用过@JsonIgnore 吗?
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多