2012-01-12 67 views
0

我有以下問題:我想通過HQL從孩子中檢索父母。HQL從沒有孩子的父母那裏獲得父母的知識

Parent.hbm.xml

<hibernate-mapping> 
<class name="Parent" table="Parent"> 
    <id name="uuid" type="java.lang.String" unsaved-value="null" access="field"> 
     <column name="uuid" not-null="true" /> 
     <generator class="org.hibernate.id.UUIDGenerator" /> 
    </id> 

    <list name="events" table="ParentToChild" cascade="all-delete-orphan" lazy="false"> 
     <key column="parentUuid" /> 
     <index column="idx" /> 
     <one-to-many class="Child" /> 
    </list> 

</class> 

<query name="findParentByChild"> 
    <![CDATA[ 
     select p from Parent as p, Child as c where c.uuid = :uuid and p.uuid = c.parentUuid 
     ]]> 
</query> 
</hibernate-mapping> 

Child.hbm.xml

<hibernate-mapping> 
<class name="Child" table="Child"> 

    <id name="uuid" type="java.lang.String" access="field"> 
     <column name="uuid" not-null="true" /> 
     <generator class="org.hibernate.id.UUIDGenerator" /> 
    </id> 

    <other properties..> 
    </class> 
</hibernate-mapping 

我可以看到生成的表爲孩子包含parentUuid列,所以我的問題是我如何在HQL中做到這一點?我可以在HQL中使用外鍵嗎?

回答

2

您可以使用加入:

select p Parent from Parent p join p.events c where c.uuid = :uuid