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.  [ 5 posts ] 
Author Message
 Post subject: Recursive join fetching not working
PostPosted: Mon Jul 10, 2006 3:42 pm 
Newbie

Joined: Fri Jun 16, 2006 3:41 pm
Posts: 18
Hi,
I have a parent object that contains a set of objects each of which contains another set of objects. All of these relationships are 1:N. I have 3 parent objects in the database (MachineClass). However when I run my query the list returns 23 of these top level objects and each one contains all of the information.

The query I run is:
List list = session.createQuery("select mc " +
"from MachineClass as mc " +
"inner join fetch mc.MachineDataInstanceSet mdis " +
"left join fetch mdis.OperationModeSet").list();

which is exactly the same the same as the sample query in the documentation section 14.3
from Cat as cat
inner join fetch cat.mate
left join fetch cat.kittens child
left join fetch child.kittens

Note that as a workaround right now, I iterate through all of the objects and call Hibernate.initialize() on the sets of each object. It works, but I don't understand the details about the proxy in the documentation section 19.1.3.

Also can I use "order by mdis.Name". I'm using sets which aren't ordered by definition, but I want to sort my output.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 5:46 am 
Newbie

Joined: Tue Jul 11, 2006 5:14 am
Posts: 10
Location: Paris, France
Have you tried with a distinct:

List list = session.createQuery("select distinct(mc) " +
"from MachineClass as mc " +
"inner join fetch mc.MachineDataInstanceSet mdis " +
"left join fetch mdis.OperationModeSet").list();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 9:21 am 
Newbie

Joined: Fri Jun 16, 2006 3:41 pm
Posts: 18
I just tried it and it didn't work. I don't have any duplicate entries in the database, it's the join that's causing the problem.

If I only run this:
from MachineClass as mc inner join fetch mc.MachineDataInstanceSet
then I get the results that I want, all initialized properly. But then I have to initialize the other set using a loop.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 9:54 am 
Newbie

Joined: Tue Jul 11, 2006 5:14 am
Posts: 10
Location: Paris, France
Many things come to me, would it be possible to show us the tables definitions and the sql's generateds by hibernate? and also whiche database you use
Because normally it will return a row for each complete set of data that works for example:
cat1 has 2 kittens
It will find cat1 for the first kitten and after, another time cat1 for the second kitten
Like this the more tables you add the worst it becomes specially with the outer joins. Thats why normally with distinct you can solve it

About your order problem, I had the same 2 weeks ago, to order by a column you must add it to the select clause, which isn't to nice since your list becomes a list of object arrays instead of a lis of your beans
What I do is add this extra column after what i'm searching off and once i have the list:
Code:
Iterator i = query.list().iterator();
resultat = new ArrayList();
         while (i.hasNext()) {
            Object[] tuple = (Object[]) i.next();
            resultat.add(tuple[0]);
         }

and then I have resultat which is the list of what i needed and it is ordered
I hope it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 2:42 pm 
Newbie

Joined: Fri Jun 16, 2006 3:41 pm
Posts: 18
Yeah I tried to avoid tuples because I'm using hibernate. After all the whole point is to deal with objects, and I've spent all this time making the beans just so I can go back to using essentially JDBC.

But that method would work.

Right now I'm using the returned Set, sticking it into a list and then calling Collections.sort() on the list (having implemented a Comparator interface). This works too.

But the I still don't understand why my query doesn't automatically fill in the objects that I joined.


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