I have in my mysql databse 2 tables :
1.teacher(id(pk),name,phone,email)
2.student(id(pk),teacher'sid(FK),name,phone,email)
The student table has a Foreign Key , teacher'sid ,that refrences to teacher.id .
I want to select the teachers that their id(teachers.id) is same as (student.teacher'sid) where student.name = "Steven".
I have made two classes with the same class name and properties as the table,and i have configured the mapping.
@Entity
class teacher {
@Id
int id;
String name;
String phone;
String email;
}
@Entity
class student {
@Id
int id;
int teachersid;
String name;
String phone;
String email;
}
I am able to select the student that his name is "steven" by:
Criteria q = session.createCriteria(student.class).add(
Restrictions.eq("name", "stevens"));
How can retreive the teacher of "Steven"? Something like
(select * from teacher(where teacher.id = student.teachersid(where student.name="stevens")))
,but with Hibernate Criteria Api.
Thanks in advance!
Criteria c = s.createCriteria(Teacher .class,"tchr");
c.createCriteria("students", "s");//Teacher class should contain students collection
c.add(Restrictions.eq("s.name", "Ashok"));
List l=c.list();
Hope this will help you. Let me know if you have any questions.