-->
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 two tables using criteria
PostPosted: Wed Feb 09, 2011 6:32 pm 
Newbie

Joined: Wed Feb 09, 2011 5:43 pm
Posts: 1
Hi,

I have two tables that share the same heading_key column. I am using the following statement using criteria to retrieve the headingcode:

org.hibernate.Criteria criteria = getPersistenceEngine()
.getPersistenceSession().getHibernateSession()
.createCriteria(Heading.class, "h")
.createCriteria("customer", "c");

criteria.setFetchMode("customer", FetchMode.JOIN)
.add(Restrictions.eq("h.headingKey", "c.headingKey" ))
.add(Restrictions.eq("c.customerId", Long.parseLong(customerId)))
.setProjection(Projections.distinct(Projections.projectionList()
.add(Projections.property("h.headingCode").as(headingCode"))));

list = criteria.list();

my xml files are defined:

customer:

<hibernate-mapping>
<class table="customer" name="com....domain.Customer">
<id
name="headingKey"
column="heading_key"
type="long"
unsaved-value="null" >
<generator class="native">
</generator>
</id>

<property
name="customerId"
not-null="false"
type="long"
column="customer_id" />


Heading:
<hibernate-mapping>
<class table="heading" name="com....domain.Heading">
<id
name="headingKey"
column="heading_key"
type="long"
unsaved-value="null" >
<generator class="native">
</generator>
</id>
<property
name="headingCode"
not-null="false"
type="java.lang.String"
column="heading_code" />

<set
name="customer" >
<key column="heading_key"/>
<one-to-many
class="com....domain.Customer" />
</set>

The query I get from hibernate is:

select
distinct this_.heading_code as y0_
from
heading this_,
customer c1_
where
this_.heading_key=c1_.heading_key
and this_.heading_key=?
and c1_.customer_id=?

Which is what I want minus the second to the last line. What I am doing wrong?

Thanks for your help.


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.