114

I keep trying variations of this query and can't seem to make this happen. I've also referenced this post: Path Expected for Join! Nhibernate Error and can't seem to apply the same logic to my query. My User object has a UserGroup collection.

I understand that the query needs to reference entities within the object, but from what I'm seeing I am...

@NamedQuery(
  name = "User.findByGroupId",
  query =
    "SELECT u FROM UserGroup ug " +
    "INNER JOIN User u WHERE ug.group_id = :groupId ORDER BY u.lastname"
)
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Ben
  • 60,438
  • 111
  • 314
  • 488

3 Answers3

144
select u from UserGroup ug inner join ug.user u 
where ug.group_id = :groupId 
order by u.lastname

As a named query:

@NamedQuery(
  name = "User.findByGroupId",
  query =
    "SELECT u FROM UserGroup ug " +
    "INNER JOIN ug.user u WHERE ug.group_id = :groupId ORDER BY u.lastname"
)

Use paths in the HQL statement, from one entity to the other. See the Hibernate documentation on HQL and joins for details.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 36
    What is a "path"? I searched the HQL documentation but did not find a definition. – jds Jul 09 '15 at 17:41
  • 9
    it means you need to link the entities: in his example here above, note how he puts ug.user u. Without the ug before it you would get the error.Also, the 'user' in 'ug.user u' should be the name of the field in Class UserGroup! – Lawrence Sep 15 '16 at 09:11
  • 9
    This HQL syntax is annoying. I had to find a lot of examples and found yours. – Bằng Rikimaru Jul 21 '17 at 10:03
  • Then I can't "manually" join entities without explicit mapping (field to be used to join) declared with the Entity? – Mr. Anderson Jul 17 '18 at 19:59
80

You need to name the entity that holds the association to User. For example,

... INNER JOIN ug.user u ...

That's the "path" the error message is complaining about -- path from UserGroup to User entity.

Hibernate relies on declarative JOINs, for which the join condition is declared in the mapping metadata. This is why it is impossible to construct the native SQL query without having the path.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • 15
    Finally someone answering the root of the issue... (the need to prefix foreign table with existing alias) solved my problem, thanks a lot ! – Saad Benbouzid Dec 16 '15 at 12:25
  • 9
    What if: you did not make an association in the Entity and just saved something like 'Long userId;' – Pwnstar Jun 11 '18 at 10:28
2

You'll be better off using where clauses. Hibernate does not accept inner joins for tables where the PK/FK relationship is not there between Entities

do

SELECT s.first_name, s.surname, sd.telephone_number FROM Student s, StudentDetails sd WHERE s.id = sd.student_id

instead of

SELECT s.first_name, s.surname, sd.telephone_number FROM Student s INNER JOIN StudentDetails sd on s.id = sd.student_id

The latter will only work if Student's id (s.id) is referenced as FK on StudentDetails (sd.student_id)) table design / erd

Pang
  • 9,564
  • 146
  • 81
  • 122