1

我在 2 个对象之间有双向关系 - 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;
}

我的休眠条件查询:

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 映射

<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 映射

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

1 回答 1

0

哎呀!2天后,我终于发现了我的错误。我忘了注册 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 值,但这是朝着正确方向迈出的一步。

于 2013-06-20T13:53:43.960 回答