File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JOIN syntax for HQL . Need help!

 
Eugene Shaforostov
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not going through the mappings; I would use a query like

 
Lisa DeSouza
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lisa DeSouza
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic