title: Getting a ClassCastException when trying a join query in Hibernate
body: <p>This is my tables:</p>
  <pre><code>@Entity
  </code></pre>
  <p>@Table(name=&quot;Students&quot;)
  public class Students {</p>
  <pre><code>@Id
  @Column(name=&quot;Student_ID&quot;)
  private int id;
  
  @Column(name=&quot;Student_Name&quot;)
  private String name;
  
  @Column(name=&quot;Email_ID&quot;)
  private String emailId;
  
  @Column(name=&quot;Phone_no&quot;)
  private long phoneNo;
  
  @Column(name=&quot;Class_ID&quot;)
  private int classId;
  
  @Column(name=&quot;DOB&quot;)
  private Date dob;
  </code></pre>
  <p>and</p>
  <pre><code>@Entity
  </code></pre>
  <p>@Table(name=&quot;Assigned&quot;)
  public class Assign {</p>
  <pre><code>@Id
  @Column(name=&quot;Assign_ID&quot;)
  private int aId;
  
  @Column(name=&quot;Class_ID&quot;)
  private int classId;
  
  @Column(name=&quot;Teacher_ID&quot;)
  private int tId;
  
  @Column(name=&quot;Subject_ID&quot;)
  private int sId;
  </code></pre>
  <p>When I run this code :</p>
  <pre><code>PrintWriter out = response.getWriter();
      out.append(&quot;&lt;html&gt;&lt;body&gt;&quot;);
  
      SessionFactory sf = HibernateUtil.getSessionFactory();
  
      Session session = sf.openSession();
      
      List&lt;Subjects&gt; subjects = session.createQuery(&quot; from Subjects&quot;).list();
      
      out.println(&quot;&lt;h1&gt; Students List :-&quot;);
      out.println(&quot;&lt;table&gt;&quot;);
      
      out.println(&quot;&lt;tr&gt;&quot;);
      out.println(&quot;&lt;th&gt; Subject Id &lt;/th&gt;&quot;);
      out.println(&quot;&lt;th&gt; Subject Name &lt;/th&gt;&quot;);
      out.println(&quot;&lt;/tr&gt;&quot;);
      
      for(Subjects subject : subjects ) {
          out.println(&quot;&lt;tr&gt;&quot;);
          out.println(&quot;&lt;td&gt;&quot;+subject.getsId()+&quot;&lt;/td&gt;&quot;);
          out.println(&quot;&lt;td&gt;&quot;+subject.getsName()+&quot;&lt;/td&gt;&quot;);
          out.println(&quot;&lt;/tr&gt;&quot;);
      }
      out.println(&quot;&lt;/table&gt;&quot;);
      out.append(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);
  </code></pre>
  <p>It works just fine But when I try to run this:</p>
  <pre><code>PrintWriter out = response.getWriter();
      out.append(&quot;&lt;html&gt;&lt;body&gt;&quot;);
  
      SessionFactory sf = HibernateUtil.getSessionFactory();
  
      Session session = sf.openSession();
      
      List&lt;Subjects&gt; subjects = session.createQuery(&quot;from Subjects s, Assign a where s.sId = a.sId and a.classId = 2&quot;).list();
      
      out.println(&quot;&lt;h1&gt; Students List :-&quot;);
      out.println(&quot;&lt;table&gt;&quot;);
      
      out.println(&quot;&lt;tr&gt;&quot;);
      out.println(&quot;&lt;th&gt; Subject Id &lt;/th&gt;&quot;);
      out.println(&quot;&lt;th&gt; Subject Name &lt;/th&gt;&quot;);
      out.println(&quot;&lt;/tr&gt;&quot;);
      
      for(Subjects subject : subjects ) {
          out.println(&quot;&lt;tr&gt;&quot;);
          out.println(&quot;&lt;td&gt;&quot;+subject.getsId()+&quot;&lt;/td&gt;&quot;);
          out.println(&quot;&lt;td&gt;&quot;+subject.getsName()+&quot;&lt;/td&gt;&quot;);
          out.println(&quot;&lt;/tr&gt;&quot;);
      }
      out.println(&quot;&lt;/table&gt;&quot;);
      out.append(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);
  </code></pre>
  <p>it gives this error :
  java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class entity.Subjects ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; entity.Subjects is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @7aeb7c5e)</p>
  <p>I didn't do @Manytoone Annotation as I read here( <a href="https://stackoverflow.com/questions/15677203/how-to-make-this-select-from-two-joined-tables-query-in-hql-form">How to make this select * from two joined tables query in HQL form?</a>) you can also work without them.</p>
  <p>I have tried various things but to no avail. Can someone please tell me why this is happening and how to fix this?</p>