Reputation: 5
i need some help i have new to hibernate i have joined three tables using inner joins which all are having one to one mapping i am getting the result in list but, how to iterate in that list. i have tried using iterator but nothing helps please help me here is the code snippet.
@Entity
public class Laptop {
@Id
private int lid;
private String lname;
//getters and setter
@Entity
public class Validity{
@Id
private int lid;
private String validitydate;
//getters and setter
@Entity
public class Student {
@Id
private int lid;
private int rollno;
private String name;
private int marks;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="lid")
private Laptop laptop;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="lid")
private Validity validity;
//getters and setters
Query that i am firing
String hql = "Select s from Student s inner join s.laptop left join
s.validity where s.lid = 1 and s.marks = 45";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
Iterator it = list.iterator();
Object[] obj = list.toArray();
while(it.hasNext())
{
Object ob = (Object)it.next();
Student s = (Student)ob;
System.out.println("Student id is "+s.getLid());
Laptop l = (Laptop)ob; // gives me error here cannot cast laptop to
student
System.out.println("laptop id is "+l.getLid());
System.out.println("laptop name is "+l.getLname());
}
Upvotes: 0
Views: 830
Reputation: 5
Iterator it = list.iterator();
Object[] obj = list.toArray();
while(it.hasNext())
{
Object ob = (Object)it.next();
Student s = (Student)ob;
System.out.println("Student id is "+s.getLid());
Laptop l = s.getLaptop();
System.out.println("laptop id is "+l.getLid());
System.out.println("laptop name is "+l.getLname());
Validity v = s.getValidity();
System.out.println("Validity id is "+v.getLid());
System.out.println("Validity Date is "+v.getValiditydate());
}
Upvotes: 0
Reputation: 692023
Your query is
Select s from Student s where ...
So it selects instances of Student.
So the result is a List<Student>
, not a List<Object[]>
.
Use the right type, and use typed queries:
Query<Student> query = session.createQuery(hql, Student.class);
List<Student> list = query.list();
for (Student student : list) {
...
}
Upvotes: 1