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.  [ 2 posts ] 
Author Message
 Post subject: Can someone help me construct this query?
PostPosted: Fri Aug 10, 2007 7:31 pm 
Newbie

Joined: Thu Jun 14, 2007 6:14 pm
Posts: 15
I'm still getting confused over how to use the HQL query language to do this.

I have a user table and a profile table that are linked by uid, and a company table that has a cid and a uid.

What Im trying to do is return all users for a specfic cid, but I also want all of the users to have their corresponding profile information in the objects as well. Do i need a special mapping for this, or can I simply use a query bring back this result?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 12, 2007 6:36 am 
Newbie

Joined: Sat Oct 28, 2006 6:16 am
Posts: 17
That depends on how you have mapped the associations between the three entities. I am going to assume that you have not mapped the associations between the entities at all. That is, there is no mapping for the one-to-one assocation between User and Profile, and there is no mapping for the one-to-many assocation between User and Company.

I expect the query you want will look something like this:

Code:
select user, profile
from User as user, Company as company, Profile as profile
where user.uid = profile.uid and company.uid = user.uid and company.cid = ?


This query will return a list of Object[]. Each Object[] will be of size 2. Index 0 will retrieve the User, 1 the Profile.

If you have mapped the associations then the query will be simplier. With the associations mapped your classes will probably look something like:

Code:
public class User {
    ...
    public Profile getProfile() {
    ....
    public Set<Company> getCompanies() {
    ...
}
public class Company {
    ...
    public User getUser() {
    ....
}


In this case the query will be something like:

Code:
select user
from User as user inner join user.companies as company where company.cid = ?


To get access the profile assocated with the user you can simply call user.getProfile().

For more information on mapping one-to-one and one-to-many assocations have a look at: [url]
http://www.hibernate.org/hib_docs/v3/re ... n-onetoone[/url] and http://www.hibernate.org/hib_docs/v3/reference/en/html/collections.html

For more information on HQL have a look at: http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html


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