-->
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.  [ 7 posts ] 
Author Message
 Post subject: many-to-many criteria query problem
PostPosted: Mon Mar 07, 2005 10:43 pm 
Newbie

Joined: Mon Mar 07, 2005 10:31 pm
Posts: 3
Hi,

I feel like I’m missing something here. I’m trying to use the criteria API to perform a query on a many to many association. I have a many to many relationship between Listing and Area. At the start of my query, I have a collection of Area objects. I want to simply return all listings that are located in any of these areas.

Logically I want:
from Listing l where l.areas in (:areas)

Where the :areas parameter would be replace by a collection of Area objects. If I were writing it in SQL, it would be:

select l.*
from Listing l inner join Listing_Area la on l.listing_id = la.listing_id
where la.area_id in (?,?,?,?)

I’ve tried several variations. Here are some below:

Variation 1:
Code:
Set areas = …   // a collection of Area objects
Criteria criteria = session.createCriteria(Listing.class);
criteria.add(Expression.in(“areas”, areas));

return criteria.list();

Variation 2:

Code:
Set areas = …   // a collection of Area objects
Criteria criteria = session.createCriteria(Listing.class);
criteria.addCriteria(“areas”).add(Expression.in(“areas”, areas);

return criteria.list();



Hibernate version:
2.1.8

Mapping documents:
Listing.hbm.xml:
<hibernate-mapping>
<class name="Listing" table="LISTING">
<id name="id" column="LISTING_ID" type="int">
<generator class="seqhilo">
<param name="sequence">LISTING_SEQ</param>
<param name="max_lo">25</param>
</generator>
</id>
<set name="areas" lazy="false" table="LISTING_AREA">
<key column="LISTING_ID"/>
<many-to-many class="Area" column="AREA_ID"/>
</set>
</class>
</hibernate-mapping>

Area.hbm.xml:
<hibernate-mapping package="com.cleanoffer.domain.mls">
<class name="Area" table="AREA">
<id name="id" column="AREA_ID">
<generator class="assigned"/>
</id>
</class>
</hibernate-mapping>



Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 10g

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Code:


Top
 Profile  
 
 Post subject: Works in HQL
PostPosted: Tue Mar 08, 2005 8:35 pm 
Newbie

Joined: Mon Mar 07, 2005 10:31 pm
Posts: 3
I tried the following in HQL:

Code:
                Query query = getHibernateTemplate().createQuery(session, "select l from Listing l join l.areas area where area in (:areas)");
                query.setParameterList("areas", areas, Hibernate.entity(Area.class));


This query appeared to work fine. I can't get the same query to work with the criteria API. Any suggestions?


Top
 Profile  
 
 Post subject: I am having the same problem too
PostPosted: Tue Mar 22, 2005 12:03 pm 
Newbie

Joined: Thu Apr 01, 2004 9:00 pm
Posts: 4
Hi All,

I am having the same problem too. I have a class Field which has a many-to-many relationship to a class Datasource. I have tried to write the criteria queries the same way. Does anyone know how to do a in query with a many-to-many association?

Thanks,
Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 1:12 pm 
Newbie

Joined: Tue May 25, 2004 9:56 am
Posts: 19
Use
Code:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
or
Code:
criteria.setResultTransformer(Criteria.ROOT_ENTITY);


I'm not sure the exact difference between the two. It seems that if the list doesn't duplicate objects, they act the same.


Top
 Profile  
 
 Post subject: Use Criteria for many-to-many relationship
PostPosted: Fri Oct 21, 2005 1:04 am 
Newbie

Joined: Sun Oct 16, 2005 11:38 pm
Posts: 5
Hi all!

I think this Criteria can help you

Code:
Criteria crit = session.createCriteria(Listing.class);
crit.createAlias("Areas", "area");
crit.add(Expression.in("area.Id", areas));


You can read Hibernate In Action for more infomation

Cheers :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 9:18 am 
Newbie

Joined: Wed Nov 02, 2005 9:11 am
Posts: 1
Hm, i have nearly the same problem, the setResultTransformer didn't help any.

I use something like:

Code:
   crit.add(Expression.in("Areas",areas));


Which does not work at all, giving an error like "didn't pass a value for parameter 1"

But when i do something like

Code:
    crit.createCriteria("areas").add(Expression.in("areaID",new Long[] {100l,31l}));


where the longs are the primary keys for the area, it works perfectly, but its not very usefull (have to traverse through the set to get an array of the keys ...)

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 1:12 pm 
Newbie

Joined: Tue May 25, 2004 9:56 am
Posts: 19
mglauche wrote:
Hm, i have nearly the same problem, the setResultTransformer didn't help any.


Did you try setResultTransformer with DISTINCT_ROOT_ENTITY? I was only using ROOT_ENTITY until some cases came up in which is still had duplicates. I changed to DISTINCT_ROOT_ENTITY and its been working fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.