我遇到以下问题:我想通过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中使用外键吗?
答案 0 :(得分:2)
您可以使用join:
select p Parent from Parent p join p.events c where c.uuid = :uuid