1

我有两个实体:“父母”和“孩子”

Child 像这样在 Parent 中映射:

代码:

 <many-to-one name="child" class="org.demo.Child"
     update="false" insert="false" embed-xml="false" node="chd/@id" >
     <column name="CHILD_ID" precision="10" scale="0"   not-null="true" />
  </many-to-one>

并且 Child 有一个像这样映射的 Enum 类型:

代码:

<property name="toyType">
     <column name="TOY_TYPE" length="100" />
     <type name="org.demo.type.LabelEnumType">
        <param name="enum">org.demo.ToyType</param>
        <param name="defaultLabel"></param>
     </type>
  </property>

在 CHILD.TOY_TYPE 列中映射为“字符串”

一切正常,但我不能这样做:

代码:

  DetachedCriteria dc = DetachedCriteria.forClass(Parent.class);
  dc.add(Restrictions.and(
           Restrictions.eq("child.id", childId),
           Restrictions.eq("child.toyType", ToyType.CAR)));
  dc.setProjection(Projections.rowCount());
  int count = DataAccessUtils.intResult(getHibernateTemplate().
        findByCriteria(dc));

因为我得到了:

nested exception is org.hibernate.QueryException: could not resolve property: 
    child.toyType of: org.demo.Parent

所以看起来它无法解决:

parent->child->toyType

可能是因为 ToyType 没有自己的“实体”,但它是嵌入的。

有什么解决方法吗?我需要继续使用 DetachedCriteria,因为它将在代码的其他地方“装饰”。所以我想知道我是否可以总是使用 DetachedCriteria 来解决这个问题。

谢谢,兰德

4

0 回答 0