我在 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>