Question

I have done my fair amount of research and finally decided to ask this.

I have two classes like this:

Employee
-emp_id
-name
-dep_id

Department
-dep_id
-name

I'm using this code to query by example:

List<Employee> find = null;
Example example = Example.create(criteria)
        .excludeZeroes()
        .ignoreCase();
find = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Employee.class)
        .add(example)
        .list() ;
return find;

The criteria object is an instance of Employee and I want to retrieve all employees with a given department name.

The problem is that when I execute the code I get employees from all departments even when criteria has a property set like this: criteria.department.name = "IT"

It works correctly when the example has parent properties set but it does not filter on children properties.

From what I can see I have to create aliases to join child properties but that kind of defeats the purpose of the Example criteria.

Any comments on this?

Était-ce utile?

La solution

A criteria created with createCriteria(Department.class) won't return employees. It will return Departments.

And an example query is used to retrieve entities which are the same as the example you pass as argument. But, as the documentation says:

Version properties, identifiers and associations are ignored

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top