Hibernate version: 3
Hello.
Say I have two tables PICTURE, TAG and a many-to-many relation table for them MTM_PICTURE_TAG.
Here is a simple declaration:
Code:
PICTURE
(id INT PRIMARY KEY,
name VARCHAR(100),
file VARCHAR(200)
)
TAG
(id INT PRIMARY KEY,
name VARCHAR(50)
)
MTM_PICTURE_TAG
(id_picture INT,
id_tag INT
)
The hibernate mapping for Tag class is obvious, let's look at the Picture's class mapping, namley at the Tags collection mapping:
Code:
.. skipped
<set name="tags" table="MTM_PICTURE_TAG">
<key column="id_picture"/>
<many-to-many column="id_tag" class="Tag"/>
</set>
.. skipped
And now I want to implement the following sql-query in terms of the Hib's Criteria API:
Code:
SELECT p.* FROM picture p JOIN mtm_picture_tag pt ON p.id = pt.id_picture WHERE pt.id_tag in (1, 2, 3)
At first, I thought that it will be as simple as
Code:
Set tags = new HashSet();
// do some tags' initialization here
List pics = session.createCriteria(Picture.class).add(Property.forName("tags").in(tags)).list();
But this doesn't work at all. The sql generated by Hib is:
Code:
select this_.id as id1_0_, this_.name as name1_0_, this_.file as file1_0_ from picture this_ where this_.id in (?, ?, ?)
So Hib is trying to apply the "IN" operator of the criteria query to the wrong property.
Any thoughts?
Thanks in advance,
Grigory.