使用Jackson 2.2将Hibernate结果序列化为JSON字符串,无限循环

时间:2013-06-19 16:44:42

标签: json hibernate jackson hibernate-criteria

我有两个对象之间的双向关系--EvalMasterEvaluation(一个)和EvalMasterEvalDetail(很多)。我已经尝试了许多不同的解决方案,但我必须将它们实现错误。我的目标是返回给定eval id的详细信息列表。

我正在使用Jackson 2.2(使用databind和datatype-hibernate3库),hibernate 3.6和spring 3.2.1。省略了一些代码以使其更易于阅读。

父类:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class EvalMasterEvaluation implements java.io.Serializable {

    private BigDecimal id;
    @JsonManagedReference private Set<EvalMasterEvalDetail> evalMasterEvalDetails = new HashSet<EvalMasterEvalDetail>(
            0);
}

儿童班:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class EvalMasterEvalDetail implements java.io.Serializable {
    private BigDecimal id;
    @JsonBackReference private EvalMasterEvaluation evalMasterEvaluation;
    private String active;
}

我的Hibernate Criteria查询:

Criteria criteria = session.createCriteria(EvalMasterEvalDetail.class)
                    .add(Restrictions.eq("active", "Y"))
                    .setFetchMode("evalMasterEvaluation", FetchMode.JOIN)
                    .createAlias("evalMasterEvaluation", "eval")
                    .add(Restrictions.eq("eval.id", evalId))
                    .addOrder(Order.asc("detailOrder"));

            details = criteria.list();

            ObjectMapper mapper = new ObjectMapper();
            json = mapper.writeValueAsString(details);

EvalMasterEvaluation mapping

<hibernate-mapping>
    <class name="org.anes.surveys.domain.EvalMasterEvaluation" table="EVAL_MASTER_EVALUATION">
        <id name="id" type="big_decimal">
            <column name="ID" precision="22" scale="0" />
            <generator class="sequence-identity" >
                <param name="sequence">EVAL_MASTER_EVALUATION_SEQ</param>
            </generator>
        </id>
        <set name="evalMasterEvalDetails" table="EVAL_MASTER_EVAL_DETAIL" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="MASTER_EVAL_ID" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="org.anes.surveys.domain.EvalMasterEvalDetail" />
        </set>
    </class>
</hibernate-mapping>

EvalMasterEvalDetail mapping

<hibernate-mapping>
    <class name="org.anes.surveys.domain.EvalMasterEvalDetail" table="EVAL_MASTER_EVAL_DETAIL">
        <id name="id" type="big_decimal">
            <column name="ID" precision="22" scale="0" />
            <generator class="sequence-identity" >
                <param name="sequence">EVAL_MASTER_EVAL_DETAIL_SEQ</param>
            </generator>
        </id>
        <many-to-one name="evalMasterEvaluation" class="org.anes.surveys.domain.EvalMasterEvaluation" fetch="select">
            <column name="MASTER_EVAL_ID" precision="22" scale="0" not-null="true" />
        </many-to-one>
        <property name="active" type="string">
            <column name="ACTIVE" length="1" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

AYE!两天后我终于找到了我的错误。我忘了注册Hibernate3Module。我的条件/映射器代码现在是:

Criteria criteria = session.createCriteria(EvalMasterEvalDetail.class)
                    .add(Restrictions.eq("active", "Y"))
                    .setFetchMode("evalMasterEvaluation", FetchMode.JOIN)
                    .createAlias("evalMasterEvaluation", "eval")
                    .add(Restrictions.eq("eval.id", evalId))
                    .addOrder(Order.asc("detailOrder"));

            details = criteria.list();

            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(new Hibernate3Module());
            json = mapper.writeValueAsString(details);

缺少evalMasterEvaluation值,但这是朝着正确方向迈出的一步。