-->
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.  [ 2 posts ] 
Author Message
 Post subject: Criteria API, forcing JOIN syntax on double join.
PostPosted: Mon Jul 03, 2006 10:00 am 
Newbie

Joined: Fri Nov 11, 2005 1:47 pm
Posts: 2
Hibernate version: 3.1
Full stack trace of any exception that occurs:
Name and version of the database you are using: MySQL 5.0

I have a parent record PARENT, and a child record CHILD (many-to-one, lazy).

Imagine I wish to retrieve a list of ParentRecords and all their (ChildRecords (all child records loaded JOIN style, so 1 query) where there exists at least one child record where child.someProperty is true.

Right now, I can get all parent rows, but loading the child rows requires an additional SQL select to load the children that did not have.

I need to figure out how to force a second join to child and make sure they end up in the result list.

Is it possible to navigate the same association twice in a criteria query?

Thx,

-JFD


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 4:23 pm 
Newbie

Joined: Tue May 10, 2005 5:29 pm
Posts: 3
The answer to your question is No, you can't make more than one join to a table using the Criteria API. You'll get a duplicate association path error when you attempt to execute your query.

I managed to get around this by using subqueries. I have a contact association table in my system which holds references to every different type of contact which can be associated with an order (shipping company, billing company, sales rep, etc...).

I used Detached criteria to accomplish this.

Code:
Criteria crit = session.createCriteria(InsertionOrder.class);
DetachedCriteria srCrit = DetachedCriteria.forClass(InsertionOrderContact.class);


Then I add my restrictions to my detached criteria object:
Code:
srCrit.setProjection(Projections.distinct(Projections.projectionList().add(Projections.property("insertionOrder.id"))));
srCrit.add(Restrictions.eq("individualRole.code","SR"));
srCrit.add(Restrictions.eq("primarySrFl","1"));
srCrit.add(Restrictions.in("individual.id",PDSUtils.makeLongArray(temp)));


Finally, I add my detached criteria object as a subquery to my main criteria object:

Code:
crit.add(Subqueries.propertyIn("id",srCrit));


I do this up to 4 different times for that one class (InsertionOrderContact.class).

Hope this helps. Good luck.


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