-->
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: Criteria query and "IN" operator for mapped collec
PostPosted: Fri Dec 08, 2006 7:29 am 
Newbie

Joined: Wed Sep 20, 2006 6:33 am
Posts: 5
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.


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.