-->
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.  [ 4 posts ] 
Author Message
 Post subject: loading collections for an entity
PostPosted: Tue Apr 18, 2006 8:52 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
Hi.

Say I have these entities:
Code:
A {

}

B {
   private A a;
}

C {
   private B b;
}


In other words, an A "has" many B's and a B "has" many C's.

Can I do this?

Code:
A {
   @OneToMany(mappedBy = "b.a")
   public Collection<C> getCs();
}


I was expecting to, but it seems I'm wrong. It would mean I wouldn't have to write a boring EJBQL queries or do iterations just to get the C's for an A.

If you can't, how do you usually handle those cases in your apps? If I write a method in A to retrieve the C's, it would mean I have to load and iterate the entire graph of B's first, which doesn't sound right. Retrieving them through queries in session beans is clumsy and plus you lost the OO approach advantages (like being able to write #{myA.cs} wherever I need those in the presentation pages).

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 10:08 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
What OO approach?

What you want is a total non OO approach.

Use simple HQL:
select distinct c from A a, B b, C c where c.b = b and b.a = a and a.id = ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 10:50 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
Say a User has many Accounts, each of which has many Mailboxes, each of which has many Folders, each of which has many Messages.

I supposed I could have something like this on User:

Code:
@OneToMany(mappedBy="folder.mailbox.account.owner")
public Collection<Message> getMessages()


and I was kinda surprised that I can't, because it would have been much more convenient than writing queries in backing beans (or iterating the entire graph programatically). But I guess it was just wishful thinking.


Top
 Profile  
 
 Post subject: Still can save a little work
PostPosted: Fri Nov 24, 2006 1:58 pm 
Newbie

Joined: Mon Oct 24, 2005 6:34 pm
Posts: 14
Location: Guadalajara, Jalisco. México.
... try using named Queries

Code:
@NamedQueries({
    @NamedQuery( name = "getMessagesOfUser"
            , query = "SELECT DISTINCT m FROM Messages AS m WHERE m.folder.mailbox.account.lots.of.things.user.id = :user " )
})


Hibernate should take care of all joining and this is still an easy to use feature.


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