samedi 14 mars 2015

Hibernate DetachedQuery and Spring's HibernateTemplate with Restriction giving wrong results

My data structure is like this



Department
-> Employees
-> Gender
-> CityID -> Cities
->CityID
->CountryID -> Countries
-> CountryID


I build Crteria like this:



DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
DetachedCriteria detlCrit = criteria.createCriteria("employees");
detlCrit.add(Restrictions.eq("gender", "MALE"));
detlCrit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);


I have one Department, 2 Employees in the Tables (one male, one female). When I excecute this criteria iam expecting Hibernate build one 'Department' object, one 'Employee' object, and city, country etc.,


But what iam getting is one Department, 2 Employees.


When I see the queries executed by Hibernate in logs, it shows two queries

First Query

->



Select * from Department, Employee
Left outer join City on Employee.cityID = City.cityID
Left outer join Country on City.countryID = City.countryID
Where Employee.DeptID = Department.DeptID
AND Employee.Gender = 'MALE';


Second query:



Select * from Employee
Left outer join City on Employee.cityID = City.cityID
Left outer join Country on City.countryID = City.countryID
Where Employee.DeptID = Department.DeptID;


Second query is wrong there is no Restriction applied on Gender='MALE';


What iam doing wrong? any suggestions? how to solve this?


sorry queries may be not exactly correct, but you got the idea. Any more details needed please ask, I can provide.


Thanks in advance..


Aucun commentaire:

Enregistrer un commentaire