-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Querying Composite Key inner Entity properties
PostPosted: Sun Jul 31, 2011 2:12 pm 
Newbie

Joined: Sat Jun 17, 2006 5:28 am
Posts: 8
Location: Egypt
I have one entity called ProductTemplate with the following hibernate mapping

Code:
<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.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:

Code:
<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:

Code:
Criteria productTemplateCriteria = this.getSession().createCriteria(ProductTemplate.class);
productTemplateCriteria.add(Restrictions.in("productTemplatePk.product.id", "1"));

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

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

generates the following error:
Quote:
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 ?

_________________
Ali Abdel-Aziz
http://www.aliabdelaziz.org/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.