Reputation: 8067
I have trying to understand how does an Example query in Hibernate works.
deptId is the primary key of the departments table.
I tried this code initially:
Dept department = new Dept();
department.setDeptId(3);
//department.setDeptName("ABCD");
Criteria criteria = session.createCriteria(Dept.class).add(Example.create(department));
Executing this code, the results are not getting filtered w.r.t the rows having deptId = 3 i.e, the SQL Query is equivalent to selecting all records from Dept table where 1 = 1.
But if i consider this code:
Dept department = new Dept();
//department.setDeptId(3);
department.setDeptName("ABCD");
Criteria criteria = session.createCriteria(Dept.class).add(Example.create(department));
The results are getting filtered w.r.t the deptName i.e. ABCD
Please inform why is the Example query behaving like this.
Upvotes: 1
Views: 1726
Reputation: 90417
From the documentation , query by example (QBE) will ignore primary key.
For the simple primary key , if you know the value of the PK , you could use load()
or get()
instead.
For the composite primary key , I agree that support of primary key in QBE is necessary . However , this request is still unresolved.
Upvotes: 3