Ben
Ben

Reputation: 62454

HQL ERROR: Path expected for join

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"
)

Upvotes: 117

Views: 196064

Answers (4)

Abbas
Abbas

Reputation: 251

If you are using hibernate and you have made the configuration to connect to the database in the persistence.xml file inside the <class> </class>

<persistence-unit name="rent">

    <class>az.com.my.entities.portal.MyEntityAppEnt</class>

    <properties>
      <!-- your hibernate configuration -->
    </properties>
</persistence-unit>

Upvotes: 0

Domingos Manuel
Domingos Manuel

Reputation: 59

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

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 692073

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.

Upvotes: 146

Marko Topolnik
Marko Topolnik

Reputation: 200236

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.

Upvotes: 80

Related Questions