开发者

Querying Composite Key inner Entity properties

I have one entity called ProductTemplate with the following hibernate mapping

<hibernate-mapping default-cascade="none">
    <class name="com.stackoverflow.ProductTemplateImpl" table="PRODUCT_TEMPLATE" dynamic-insert="false" dynamic-update="false">
        <composite-id name="productTemplatePk" class="com.stackoverflow.product.ProductTemplatePK">
            <key-property name="templateType" type="java.lang.String">
                <column name="TEMPLATE_ID" sql-type="VARCHAR2(255)" not-null="true"/>
            </key-property>
            <key-many-to-one name="product" class="com.stackoverflow.ProductImpl" >
                <column name="PROD_ID"/>
            </key-many-to-one>
        </composite-id>
    </class>
</hibernate-mapping>

where ProductTemplatePK is a normal java Primary key class.

and another entity called Product with the following hibernate mapping:

<hibernate-mapping default-cascade="none">
    <class name="com.stackoverflow.ProductImpl" table="PRODUCT" dynamic-insert="false" dynamic-update="false">
        <id name="id" type="java.lang.String" unsaved-value="null">
            <column name="PROD_ID" sql-type="VARCHAR2(255)"/>
            <generator class="assigned">
            </generator>
        </id>
        <property name="state" type="java.lang.String">
                     <column name="PROD_STATE" not-null="true" unique="false"/>
        </property>
        <property name="nameEn" type="java.lang.String">
                     <column name="PROD_NAME_EN" not-null="true" unique="false"/>
        </property>
    </class>
</hibernate-mapping>

Now if I tried to retrieve all productTemplates based on the productId I can do it using the following hibernate criteria:

Criteria productTemplateCriteria = this.getSession().createCriteria(ProductTemplate.class);
productTemplateCriteria.add(Restrictions.in("productTemplatePk.p开发者_StackOverflowroduct.id", "1"));

but I don't know how to retrieve those templates based on the product.nameEn as the following code:

Criteria productTemplateCriteria = this.getSession().createCriteria(ProductTemplate.class);
productTemplateCriteria.add(Restrictions.in("productTemplatePk.product.nameEn", "Ali"));

generates the following error: Caused by: org.hibernate.QueryException: could not resolve property: productTemplatePk.product.nameEn of: com.stackoverflow.ProductTemplateImpl.

so how can I query an entity property that is mapped as part of a composite primary key ?


I manged to solve this by simple HQL query as I didn't find any way to do it using hibernate API

this is the HQL query I used:

 from com.stackoverflow.ProductTemplateImpl productTemplate
where productTemplate.productTemplatePk.product.state not in (:productStates) 

But I'm still interested to know if there is any Hibernate API combination that can do it.


I was not able to get deeper than 2 levels except for ids when specifying property names in criteria. Try using explicit join with alias:

Criteria crit = this.getSession().createCriteria(ProductTemplate.class);
crit.addAlias("productTemplatePk.product","p")
    .add(Restrictions.in("p.nameEn", "Ali"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜