-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query using multiple inner joins on the same table
PostPosted: Tue Aug 07, 2007 1:05 pm 
Newbie

Joined: Tue Aug 07, 2007 9:40 am
Posts: 2
Hello,

I'm not sure how to write a Criteria query that would use multiple inner joins on the same table.

Here is a mapping on which this problem is based:

Code:
<class name="Item" table="ITEMS">
  <id name="id" type="long" column="ID">
    <generator class="increment" />
  </id>
  <property name="name" type="string" column="Name" length="50" unique="true" />
  <set name="attributes" inverse="true">
    <key column="ItemID" />
    <one-to-many class="Attribute" />
  </set>
</class>
   
<class name="Attribute" table="ATTRIBUTES">
  <composite-id>
    <key-many-to-one name="item" column="ItemID" class="Item" />
    <key-property name="name" column="Name" type="string" length="100"/>
  </composite-id>
  <property name="value" type="java.lang.String">
    <column name="Value" length="1024" default="null"  not-null="false"/>
  </property>
</class>



The ATTRIBUTES table contains rows (IdemId, Name, Value) such as:
(1, 'X', 'A'),
(1, 'Y', 'A'),
(2, 'X', 'A'),
(2, 'Y', 'B'),
(3, 'X', 'A'),
(3, 'Y', 'C'),
(4, 'X', 'B'),
(4, 'Y', 'A'),
(5, 'X', 'B'),
(6, 'Y', 'B'),
(7, 'X', 'B')


I would like to be able to select Items based on what attributes and attribute values they
have. This would be something representing "find all the Items that have an attribute X with value A and an attribute Y with value B".

This MySQL query seems to produce the required result:

Code:
SELECT DISTINCT (I1.ID) FROM ITEMS I1
INNER JOIN ATTRIBUTES T1 ON T1.ItemID = I1.ID
INNER JOIN ATTRIBUTES T2 ON T2.ItemID = I1.ID
WHERE (T1.Name LIKE 'X' AND T1.Value LIKE 'A')
AND (T2.Name LIKE 'Y' AND T2.Value LIKE 'B')
GROUP BY I1.ID




I've tried to express a similar query using Hiberate's Criteria (based on section 15.4 of
the manual), but I can't get it to work.

I've tried the following (and a few variants):
Code:
Criteria criteria =
  session.createCriteria(Item.class)
    .createAlias("attributes","att1")
    .createAlias("attributes","att2")
    .add(Restrictions.conjunction()
      .add(Restrictions.conjunction().add(Restrictions.like("att1.name", "X")).add(Restrictions.like("att1.value","A")))
      .add(Restrictions.conjunction().add(Restrictions.like("att2.name", "Y")).add(Restrictions.like("att2.value","B")))
    );


However, criteria.list() produces an exception:
org.hibernate.QueryException: duplicate association path: attributes



Is it possible to express this type of queries using criteria? Is there another way to achieve this?


Thank you,

Bruno.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 4:35 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Criteria queries don't currently support multiple joins to the same table. There's an open JIRA issue for it here:
http://opensource.atlassian.com/project ... se/HHH-879

Try using a subquery perhaps? If you do, some of the info about criteria subqueries here might be helpful:
http://forum.hibernate.org/viewtopic.php?t=978225


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 7:02 pm 
Newbie

Joined: Tue Aug 07, 2007 9:40 am
Posts: 2
Thanks for the pointers. I wasn't initially keen on using HQL directly, but it seems to work fine (I already had the query expressions for MySQL, so it only required little adaptation).

Thanks,

Bruno.


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