-->
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: Why can't I join using a one-to-many?
PostPosted: Tue Mar 01, 2005 8:55 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
In the style of the reference docs, this in HQL doesn't work because "kittens" is a set, or list, or whatever:

select distinct cat
where cat.kittens.age > 10

So that would give me all cats who have a kitten older then 10. Or, in SQL (which does work), if you prefer:

SELECT DISCTINCT m.*
FROM cat c
INNER JOIN kitten k ON k.momID = c.id
WHERE k.age > 10

I've got many JDBC queries that I am DYING to replace to get my app running totally through Hibernate, but they are all in this style. Am I missing something? Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
umm aren't you missing a from clause ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 1:24 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Okay, my bad on that one. My example was meant to be:

select distinct cat
from Cat cat
where cat.kittens.age > 10

That still won't work, however. Here's my actual query, just so I can't mess up another example. :D

Code:
return getHibernateTemplate().find(
    "SELECT DISTINCT ep.State " +
    "FROM EzProv ep " +
    "WHERE ep.healthPlanJoin.healthPlan.hpcode = ?",
    hpcode
);


So healthplan is joined to provider as a many to many. I need to include the join table because it contains other information that I need. Here's what Hibernate has to say:

QueryException: expecting 'elements' or 'indices' after: healthPlan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 1:36 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Okay, I'm sorry, I don't think I have enough understanding of exactly what I'm doing here. I'm going to try mapping the relationship as a many-to-many and try to figure out how to get the fields of the join table that I need (component I think). Anyway, we'll see how this works when I set it up the RIGHT way. :D


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 1:42 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Okay, so I did that, which made the design a lot cleaner:

Code:
return getHibernateTemplate().find(
    "SELECT DISTINCT ep.State " +
    "FROM EzProv ep " +
    "WHERE ep.healthPlans.hpcode = ?",
    hpcode
);


however I still get the same error.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 11:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
select distinct ep.state
from EzProv ep
inner join ep.healthPlans hp
where hp.hpcode = ...

I *think* is what you are looking for, though without the mappings I am just making wild guesses ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 1:18 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Woo hoo, that worked! Thank you so much for your help, Steve. I guess the problem was that I just assumed that actually using the keyword "JOIN" was not ever necessary.


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.