I have two table here:
Student- StudentId
- StudentName
Department- DepartmentName
- DepartmentCode
- StudentId
I doing searching function in between this two table for Student
User can Query StudentName, DepartmentCode, DepartName ( 3 of them is textfield ).
Here is the Query:
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);
if(!StringUtils.isEmpty(studentName)) {
criteria.add(Restrictions.eq("studentName", studentName));
}
// Suspect line
if(!StringUtils.isEmpty(departmentName)) {
criteria.createCriteria("department")
.add(Restrictions.eq("departmentName", departmentName));
}
// Suspect line
if(!StringUtils.isEmpty(departmentCode)) {
criteria.createCriteria("department")
.add(Restrictions.eq("departmentCode", departmentCode));
}
return getHibernateTemplate().findByCriteria(criteria);
The problem is
When I search departmentName and departmentCode together , it will output departmentName result only.
So far I know when criteria.createCriteria("department") the query will go to department table and searching ( something like table linking) But this cannot work for using it more than one time.
Can anyone there pin point my mistake?
Thanks