-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate Query - joins - howto?
PostPosted: Tue Apr 27, 2010 5:30 pm 
Newbie

Joined: Tue Apr 27, 2010 5:18 pm
Posts: 19
Hello Hibernate users.

I need to do a search across several tables, lets call them A, B and C.

heres a pseudo database structure:

Code:
Table A:
ID1,
ATTRIBUTE1

Table B:
ID1,
ID2,
ATTRIBUTE2,
ATTRIBUTE3

Table C:
ID3,
ATTRIBUTE4 (=ID1),
ATTRIBUTE5


in jdbc sql I might write something like

Code:
select distinct A.ID1, A.ATTRIBUTE1 from A, B, C
where A.ID1 = B.ID1
AND A.ID1 = C.ATTRIBUTE4
AND B.ATTIBUTE2 = 'some value'
AND C.ATTRIBUTE5 = 'some other value'
AND A.ATTIBUTE1 = 'something interesting'


Obviously I'd parse my result set and create my objects for A.

Now I'd like to do the same thing in Hibernate, but I don't know where to start.
If necessary I can create some tables, hbm.xml files, etc - but at this stage it would just complicate this post.
I'm sure this must be possible, I really just need a pointer or two.

Thanks

Martin


Last edited by mac24nz on Wed Apr 28, 2010 2:33 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Hibernate Query - joins - howto?
PostPosted: Wed Apr 28, 2010 2:17 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The tutorial in the Hibernate documentation is a good place to start: http://docs.jboss.org/hibernate/stable/ ... orial.html


Top
 Profile  
 
 Post subject: Re: Hibernate Query - joins - howto?
PostPosted: Wed Apr 28, 2010 2:35 am 
Newbie

Joined: Tue Apr 27, 2010 5:18 pm
Posts: 19
So I do actually have Hibernate working for quite a few things.....I'm past the basic tutorials.
Selects using Hibernate Querys, inserts, updates, etc.
I'm just not sure how to go about a complicated join.

Do I do something like this?

Code:
select A from A,B,C
where A.ID1 = B.ID1
AND A.ID1 = C.ATTRIBUTE4
AND B.ATTIBUTE2 = 'some value'
AND C.ATTRIBUTE5 = 'some other value'
AND A.ATTIBUTE1 = 'something interesting'


Top
 Profile  
 
 Post subject: Re: Hibernate Query - joins - howto?
PostPosted: Wed Apr 28, 2010 2:37 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If you have mapping files you'll need to post them. It is hard to help you otherwise.

Edit: There is a chapter about HQL in the documentation also: http://docs.jboss.org/hibernate/stable/ ... yhql-joins


Top
 Profile  
 
 Post subject: Re: Hibernate Query - joins - howto?
PostPosted: Wed Apr 28, 2010 4:50 pm 
Newbie

Joined: Tue Apr 27, 2010 5:18 pm
Posts: 19
Actually my crazy little joins seem to work...I was so sure that it would be hopeless and that it couldn't be that easy.

Another question, lets say I want to perform a query like this:

Code:
from B where B.id IN ?


Can I do that?
Then somehow call a setCollection method?
Code:
Query.setCollection(1, thisIsMyCollection);


or do I have to convert it into a string in the query first? or create a lot of
Code:
from B where B.id IN ('this','is','a','list','of','strings')

Code:
from B where (B.id = ? OR B.id = ? OR B.id = ?....)


I haven't had any success so far....I had this same problem with JDBC too.
This can also get quite complicated if the id is a complex object - not just a simple object (like a string).

I'd appreciate any help here too.


Top
 Profile  
 
 Post subject: Re: Hibernate Query - joins - howto?
PostPosted: Wed Apr 28, 2010 5:24 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The first example is the close... You need to use named parameters in the query.

Code:
from B where B.id IN (:list)


Then, set the values with Query.setParameterList("list", thisIsMyCollection). This works as long as the collection of values is not empty. If it is empty, the where part of the query should be removed.


Top
 Profile  
 
 Post subject: Re: Hibernate Query - joins - howto?
PostPosted: Wed Apr 28, 2010 5:38 pm 
Newbie

Joined: Tue Apr 27, 2010 5:18 pm
Posts: 19
Ahh Thanks - that will make my life much easier!


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