-->
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.  [ 4 posts ] 
Author Message
 Post subject: Example Criteria query with many-to-many mapping
PostPosted: Fri Jun 30, 2006 10:17 am 
Newbie

Joined: Fri Jun 30, 2006 8:52 am
Posts: 2
Hi all,

I have a many-to-many mapping and want a Criteria query
using the Example class. But I don't know how to formulate
the query.
I found this posting http://forum.hibernate.org/viewtopic.ph ... y&start=15
which ends with no result. I tried the suggested way in the posting and ended also with a scrambled SQL query.

Is there a way to do it?

To be more specific: I have:
table x
table y
and table xy for the many-to-many mapping.

I try:
[code]
Criteria criteria = session.createCriteria(X.class,"x");
Set set = x.getYs();
Iterator iter = set.iterator();
while(iter.hasNext()){
Y y = (Y)iter.next();
Long id = y.getId();
DetachedCriteria dc = DetachedCriteria.forClass(X.class, "x2")
.setProjection(Property.forName("id")).createCriteria(
"ys", "s").add(Restrictions.idEq(id)).add(
Property.forName("x2.id").eqProperty("x.id"));

criteria = criteria.add(Subqueries.exists(dc));
}
List result = criteria.list();
[/code]
I don't know if this is the right query: I want instances of x
where y.attr1=value1 and y.attr2=value2 (these are set in y)

Please help.
Thanks, Heike

--
I am using hibernate 3.1 with postgreSQL


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 5:26 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Look at 'in elements(...)' function in HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 4:29 pm 
Newbie

Joined: Fri Jun 30, 2006 8:52 am
Posts: 2
Hi alesj,

thanks for your answer.
I tried to understand what "in elements" does. But could
not see a way how to use it with Example queries. The problem is, I have lots of attributes. So, the Example query is really a charm.
I'd rathr not use hql.
Or have I misunderstood the "in elements"?

Thanks, Heike


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 5:08 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
hfra2 wrote:
Or have I misunderstood the "in elements"?

Nope.

Uf, I haven't really used a lot of new Criteria API from H3, and I see that new util classes are poping all the time.

Maybe you could do this - create a middle entity - having there 2 one2many mapping to X and Y - and then define a filter on it - which will filter your X's depending on Y's fields.

Rgds, Ales


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.