Following DetachedCriteria
should work if your hibernate mapping is correct!
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(
Student.class, "studentAlias");
detachedCriteria.createAlias("details", "detailsAlias")// details is collection of `StudentDetail` in `Student` class
.createAlias("detailsAlias.department", "departmentAlias")//department is `Department` type variable in `StudentDetail`
.add(Restrictions.eq("detailsAlias.remarks", "x"))
.add(Restrictions.eq("departmentAlias.name", "x"));
List<Student> list = detachedCriteria
.getExecutableCriteria(hibernateSession).setMaxResults(100)
.list();
for this i created class Student
, Department
, StudentDetail
class with following annotations.
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long std_id;
@OneToMany(mappedBy = "student", fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
private List<StudentDetail> details;
// getter/setters
}
@Entity
@Table(name = "department")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Dept_ID;
private String name;
private String remarks;
// getter/setters
}
@Entity
@Table(name = "student_detail")
public class StudentDetail {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "std_id")
@Fetch(FetchMode.SELECT)
private Student student;
@ManyToOne
@JoinColumn(name = "Dept_ID")
@Fetch(FetchMode.SELECT)
private Department department;
private String remarks;
// getter/setters
}