-->
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.  [ 9 posts ] 
Author Message
 Post subject: joins
PostPosted: Tue Aug 26, 2003 9:44 pm 
Newbie

Joined: Tue Aug 26, 2003 7:12 pm
Posts: 4
Hi
I was checking to see what the syntax would be for doing joins against multiple tables.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 9:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please start reading the documentation. You don't join tables with Hibernate (thats what happens "under the hood"), you join class level associations. We have a whole chapter about HQL (Hibernates extension to SQL) in the reference documentation.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Some java code that may help
PostPosted: Tue Aug 26, 2003 11:16 pm 
Newbie

Joined: Tue Aug 26, 2003 2:31 pm
Posts: 15
Location: San Diego, CA
To do a join, I assume you already had your hibernate mapping converted to java classes with getters and setters.

If so, this is a method of joining tables.

Session s;
try
{
s = getTestSession();
Query q = sess.createQuery("select ent, cc from org.hibernate.Datablock as db inner join db.childTables as cc inner join db.entitys ent where db.datablockName = '"+ myDatablockName+"' and cc.type='stuff'");

pes = q.list();
if (pes.size() > 0)
{
iter = pes.listIterator();
while (iter.hasNext())
{
Object[] foo = (Object[])iter.next();
Entity ent = (Entity)foo[0];
ChildTable cc = (ChildTable)foo[1];
if (!ent.getSomeData().equalsIgnoreCase("You bet"))
{
String thisName = cc.getSomeName();
String formula = cc.getFormula();
}
}
}

}

Basically, the table Datablock is a parent to the other two tables. They have a relationship. So the relationship is know internally. You don't have to explicitly do a normal join statement where you would say tableA.id = tableB.id. The java class should just know how to link them, if you did your hibernate mapping correctly.

Once you join them you can access them like I did above. Really simple, very nice.

Hope this helps.

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 6:43 am 
Newbie

Joined: Tue Dec 16, 2003 6:38 am
Posts: 5
Hi,

Can someone confirm me that the use of parantheses is irrelevant
in the "from" clause of the query?

For example, if I use :

from eg.Cat as cat
join cat.mate as mate
left join cat.kittens as kitten

would I get the same result as when using:

from (eg.Cat as cat
join cat.mate as mate)
left join cat.kittens as kitten

or:

from eg.Cat as cat
join (cat.mate as mate
left join cat.kittens as kitten)

Thank you,
Teodor


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 8:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It's irrevelant since hibernate looks a the collection, not the lecft side of the join.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Problem with joining multiple tables
PostPosted: Sat Dec 11, 2004 4:01 am 
Newbie

Joined: Sat Dec 11, 2004 3:43 am
Posts: 3
hi,
I need to join 3 tables. They don't have a foreign-key relationship. How can i do the inner or outer join on the tables whose mapping files or value objects don't have a relationship. Can you please let me know as soon as possible. i am close to missing the deadline. i appreciate if you can tell me what you found out even if you didn't find a solution.

thx in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 11, 2004 7:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You cannot. Outer joins require the mapping of the relationships.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 30, 2004 8:14 pm 
Newbie

Joined: Thu Dec 30, 2004 8:06 pm
Posts: 1
so.... can you join a table that is represented by a persistent class with a table that isn't represented by a persistent class? What I have is this:
A Roles table (persistent class = Roles), a userId, and a join table (UserRoles; no associated persistent class) that has various roleIds for a userId. So, I'm coming in knowing my userId and I want to retrieve a List of Role objects by joining the Roles table with the UserRoles table where Roles.roleId = UserRoles.roleID and UserRoles.userId = userId.

Scott


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 30, 2004 8:22 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
scooter4j wrote:
so.... can you join a table that is represented by a persistent class with a table that isn't represented by a persistent class? What I have is this:
A Roles table (persistent class = Roles), a userId, and a join table (UserRoles; no associated persistent class) that has various roleIds for a userId. So, I'm coming in knowing my userId and I want to retrieve a List of Role objects by joining the Roles table with the UserRoles table where Roles.roleId = UserRoles.roleID and UserRoles.userId = userId.
Scott


I think you'll find your answer in the docos. Read up on many-to-many. (BTW, I've implemented this exact schema many times and it works just fine... you are but a few lines of code away). :)


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