-->
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.  [ 12 posts ] 
Author Message
 Post subject: where clause in HQL
PostPosted: Tue Oct 07, 2003 12:04 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
hello,

I have a question about if its necessary to use a where clause if
two tables are connected by one's primary key and other's foreign key
in their mapping files.

Example:

Department 1 : many Student
student table has foreign key towards department table's primary key.

I use the following query to get data from 2 tables:

select d.name, d.address, s.name from Department as d, Student as s
where d.id = ?

Here, I do not do something like:
s.dept_id = :deptId

since hibernate mappings already specify their one-to-many bi directional association.

I have two questions about this:
-should this work? (I got null data from the above query)
-if a department has a list of students, how to get this list of students
using a query? (For the above query, I use s.name to get a student name,
this will return multiple rows with different student names and same dept)

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 12:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
select d.name, d.address, s.name from Department as d, Student as s
where d.id = ?

select d.name, d.address, s.name from Student as s join s.department as d where d.id = ?

Quote:
how to get this list of students

from Student as s join s.department as d where d.id = ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 1:05 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
steve wrote:
Quote:
select d.name, d.address, s.name from Department as d, Student as s
where d.id = ?

select d.name, d.address, s.name from Student as s join s.department as d where d.id = ?

Quote:
how to get this list of students

from Student as s join s.department as d where d.id = ?


Thanks Steve.

I am joining 6 tables using the above technique, and I got:

"outer or full join must be followed by path expression"

Is there a different grammar for this?

thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 2:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Post the relevant mappings and a description of what you want to try to query.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 9:16 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
steve wrote:
Quote:
select d.name, d.address, s.name from Department as d, Student as s
where d.id = ?

select d.name, d.address, s.name from Student as s join s.department as d where d.id = ?

Quote:
how to get this list of students

from Student as s join s.department as d where d.id = ?


Thanks, Steve. I used the following query and successfully retrieved data:

select s.department.name s.department.address, s.name, from Student as s
where s.department.name=?

Every department has a Set of students, and I cannot use a separate query to retrieve them, I want to get a Set of students using the same query. I tried this:

select d.name d.address, d.students, from Department as d where d.id=?

but it gave me a "incorrect query syntax " error.

How can we achieve that?

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 9:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Quote:
select d.name d.address, d.students, from Department as d where d.id=?


Should this not be

select d.name, d.address, d.students from Department as d where d.id=?

Your missing a , and added an extra , .


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 10:16 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
david wrote:
Quote:
select d.name d.address, d.students, from Department as d where d.id=?


Should this not be

select d.name, d.address, d.students from Department as d where d.id=?

Your missing a , and added an extra , .


Yes, David, those were my typos, but the query still does not work.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 10:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The query you show is correct.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 10:31 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Again, this is my query, thanks for pointing out my typos:

select d.name, d.address, d.students from Department as d where d.id=?

I still got "Incorrect query syntax" error.

I am wondering fi anyone retrieved a Set of children using this kind of query.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 10:32 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh. Try elements(d.students) instead of d.students. Read up on the HQL chapter in the documentation.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 11:56 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
christian wrote:
Oh. Try elements(d.students) instead of d.students. Read up on the HQL chapter in the documentation.


Great, thanks for the pointer. I now can succesfully retrieve the many end of a one-to-many association.

Now I have a further question: if there is a many-to-may association
between Department and Professor. A department have a Set of professors,
and a professor can have a list of departments, and the association is bidirectional.

If I want to retrieve a list of professors for every department:

select d.name, d.address, elements(d.professors) from Department as d where d.id=?

I got no error but 0 rows returned. Is there a different catch for this?

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 2:27 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
sgu2 wrote:
christian wrote:
Oh. Try elements(d.students) instead of d.students. Read up on the HQL chapter in the documentation.


Great, thanks for the pointer. I now can succesfully retrieve the many end of a one-to-many association.

Now I have a further question: if there is a many-to-may association
between Department and Professor. A department have a Set of professors,
and a professor can have a list of departments, and the association is bidirectional.

If I want to retrieve a list of professors for every department:

select d.name, d.address, elements(d.professors) from Department as d where d.id=?

I got no error but 0 rows returned. Is there a different catch for this?

regards,



Is it possible to do the above?

rgds


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