-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using Criteria Query on Mapped Assosiations
PostPosted: Fri Dec 08, 2006 7:01 am 
Newbie

Joined: Fri Jul 15, 2005 12:42 am
Posts: 18
Hibernate version: 3.0.5

Mapping documents:
<class
name="ShopDTO"
table="GTShop"
dynamic-update="true"
>

<id
name="ID"
type="java.lang.String"
>
<column
name="ID"
/>
<generator class="assigned">

</generator>
</id>

<many-to-one
name="productDTO"
class="ProductMasterDTO"
cascade="none"
outer-join="true"
update="true"
insert="true"
access="property"
column="PRODUCTID"
/>
</class></hibernate-mapping>




I am getting an exception when i try to execute the below code.
Criteri cq = session.createCriteria(ShopDTO.class);
cq.add(Restrictions.eq("productDTO.ID", productCode.toString())); //ID is the PK of product//

cq.add(Restrictions.eq("productDTO.productAbbreviated", productAbbr.toString()));

cq.list();

The above code works if i comment the Restrictions.eq("productDTO.productAbbreviated", ..... part


This is the stack trace
org.hibernate.QueryException: could not resolve property: productDTO.productAbbreviated of: ShopDTO
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:63)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.BasicEntityPersister.toColumns(BasicEntityPersister.java:1086)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:403)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:369)



Is this a Known issue? we can query only on primary key of a mapped property using criteria query?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 7:52 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
To acces the property of productDTO you must join it. There're two ways of expression join in the Criteria API:

1. Use method createCriteria() of the Criteria interface:

Criteria cq = session.createCriteria(ShopDTO.class);
Criteria productCriteria = cq.createCriteria("productDTO");
productCriteria.add(Restrictions.eq("productDTO.ID", productCode.toString())); //ID is the PK of product//
productCriteria.add(Restrictions.eq("productDTO.productAbbreviated", productAbbr.toString()));

cq.list();

2. Assign an alias to the joined entity:

session.createCriteria(ShopDTO.class)
.createAlias("productDTO", "productDTO")
.add(Restrictions.eq("productDTO.ID", productCode.toString()))
.add(Restrictions.eq("productDTO.productAbbreviated", productAbbr.toString()))
.list();

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 11:50 am 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
ist the mapping productDTO property in the ShopDTO.java and do we need getter and setter for it... right...



<many-to-one
name="productDTO"
class="ProductMasterDTO"
cascade="none"
outer-join="true"
update="true"
insert="true"
access="property"
column="PRODUCTID"
/>


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.