aspose file tools*
The moose likes Object Relational Mapping and the fly likes JOIN syntax for HQL .  Need help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JOIN syntax for HQL .  Need help!" Watch "JOIN syntax for HQL .  Need help!" New topic
Author

JOIN syntax for HQL . Need help!

Eugene Shaforostov
Greenhorn

Joined: Aug 11, 2007
Posts: 6
I have two tables



I need to join this tables by News.author_id and Users.id.
When I trying to query

I get the following error:
QuerySyntaxException: Path expected for join!

Please help me to resolve this problem.
Thanks in advance for any help.
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Can you post the mappings?
See also in the Hibernate documentation, in particular that, to be joinable, the entities need to be associated.
Eugene Shaforostov
Greenhorn

Joined: Aug 11, 2007
Posts: 6
Thanks, Edvins, I already read this reference, but the question remains.

Mapping for table News
News.hbm.xml



Mapping for table Users
Users.hbm.xml
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
To use the join syntax, you need a relationship defined (I guess it is a many-to-one in this case) in the mappings. Without it, one can use a query like this:

Eugene Shaforostov
Greenhorn

Joined: Aug 11, 2007
Posts: 6
Thanks, this query works fine.
But I want to understand how use association mappings.
Mapping files should looks like this?
News.hbm.xml



Users.hbm.xml


If it`s right then what query will return required result?
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Not going through the mappings; I would use a query like

Lisa DeSouza
Greenhorn

Joined: Jul 21, 2009
Posts: 6
But is it not possible to use something equivalent to a left outer join if the entities don't contain each other as a set?

Edvins Reisons wrote:To use the join syntax, you need a relationship defined (I guess it is a many-to-one in this case) in the mappings. Without it, one can use a query like this:

Lisa DeSouza
Greenhorn

Joined: Jul 21, 2009
Posts: 6
But is it not possible to use something equivalent to a left outer join if the entities don't contain each other as a set?

This is my Category:


and this is my Organization:


Similar to the linking in Category I have linking for 2-3 other entities.
I actually needed to retrieve them using an HQL query equivalent to this in MYSQL:


The code you posted is equivalent to a basic join, right? and doesn't retrieve the data I need...

Edvins Reisons wrote:To use the join syntax, you need a relationship defined (I guess it is a many-to-one in this case) in the mappings. Without it, one can use a query like this:

Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Lisa DeSouza wrote:But is it not possible to use something equivalent to a left outer join if the entities don't contain each other as a set?


Well...there is no 'straightforward' way in hibernate to do a "Left Outer Join" when your entities do not have a relationship...

but there are hacks...

Like the following set of SQL demonstrate..



So, if it is hibernate, the above query will work....so you can get the desired result for a join even without using join .. :-)
But such a thing is not recommended, because of the performance impact that it may have if the number of joins on the table are more...and the query becomes complex...etc


Rahul Babbar
Lisa DeSouza
Greenhorn

Joined: Jul 21, 2009
Posts: 6
That's what I finally ended up doing.... Thanks!

Rahul Babbar wrote:
Lisa DeSouza wrote:But is it not possible to use something equivalent to a left outer join if the entities don't contain each other as a set?


Well...there is no 'straightforward' way in hibernate to do a "Left Outer Join" when your entities do not have a relationship...

but there are hacks...

Like the following set of SQL demonstrate..



So, if it is hibernate, the above query will work....so you can get the desired result for a join even without using join .. :-)
But such a thing is not recommended, because of the performance impact that it may have if the number of joins on the table are more...and the query becomes complex...etc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JOIN syntax for HQL . Need help!