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: