Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You wouldn't do that in Hibernate. The whole point of using hibernate is that you are dealing with objects.</p> <p>So I guess your Student class would have a List of type Grade</p> <pre><code>@Entity public class Student{ // accessors and id omitted @OneToMany(mappedBy="student") private List&lt;Grade&gt; grades; } @Entity public class Grade{ // accessors and id omitted @ManyToOne private Student student; } </code></pre> <p>You would look up the grade via <a href="http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Session.html#get%28java.lang.Class,%20java.io.Serializable%29" rel="nofollow noreferrer">session.get()</a> and then do <code>grade.getStudent()</code> to access the student:</p> <pre><code>Grade grade = (Grade) session.get(Grade.class, gradeId); Student student = grade.getStudent(); </code></pre> <p>HQL queries are designed for Scenarios that are too complex for these lookup methods.</p> <p>Edit: I just realized that the question is tagged <code>jpa</code>. Then of course you would not be using HQL, but JPQL instead. And also you'd be using this code:</p> <pre><code>Grade grade = entityManager.find(Grade.class, gradeId); // no cast needed with JPA 2 Student student = grade.getStudent(); </code></pre> <hr> <p>Edit: given the requirements you added in your comments I'd change the data model to something like this (ids omitted):</p> <pre><code>@Entity public class Student{ public List&lt;Course&gt; getCourses(){ return courses; } public void setCourses(List&lt;Course&gt; courses){ this.courses = courses; } @OneToMany(mappedBy="student") private List&lt;Course&gt; courses; } @Entity public class Course{ @ManyToOne(optional=false) private Student student; @ManyToOne(optional=false) private Grade grade; public void setStudent(Student student){ this.student = student; } public Student getStudent(){ return student; } public Grade getGrade(){ return grade; } public void setGrade(Grade grade){ this.grade = grade; } } @Entity public class Grade{ @OneToMany(mappedBy="grade") private Set&lt;Course&gt; courses; public void setCourses(Set&lt;Course&gt; courses){ this.courses = courses; } public Set&lt;Course&gt; getCourses(){ return courses; } } </code></pre> <p>And I'd query like this:</p> <pre><code>Grade grade = entityManager.find(Grade.class, 1L); List&lt;Student&gt; studentsWithThisGrade = new ArrayList&lt;Student&gt;(); for(Course course : grade.getCourses()){ studentsWithThisGrade.add(course.getStudent()); } </code></pre> <p>(But Grade should probably not be an entity, but either a numeric value or an enum.)</p> <hr> <p>It turns out that the OP uses plain hibernate after all, no JPA. So whenever you see <code>entityManager.find()</code> above, replace that in your mind with <code>session.get()</code> :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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