Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In HQL, whenever you join without an explicit select clause Hibernate returns all the joined entities. Its not the most intuitive thing, buts it is legacy behavior at this point.</p> <p>Secondly you are re-stating the join condition between school and student. You already specified that in the mapping, so Hibernate knows that when you say "join s.student" in the query. So the where clause is unnecessary here.</p> <p>You data model seems a bit backwards I think from what most think of School/Student relationship. Generally a school has multiple students (a student being a person in a particular relationship with the school). But given the model as you describe...</p> <p>You have not stated exactly what you want out of this query, but your original query is returning all students. No idea why you even "drive" the query from the School object.</p> <pre><code>Session session = ...; List&lt;Student&gt; results = (List&lt;Students&gt;) session.createQuery( "select s from Students s" ).list(); for ( Student student : results ) { // handle each student } </code></pre> <p>If you want just the names of the students (which you sort-of/kind-of imply):</p> <pre><code>Session session = ...; List&lt;String&gt; results = (List&lt;String&gt;) session.createQuery( "select s.name from Students s" ).list(); for ( String student : results ) { // handle each student name } </code></pre> <p>Note that the select clause is really optional in my above queries because there is only one entity referenced in the from clause. I prefer to always specify a select clause to be explicit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload