因此,如果我要为员工制定限制,我会说
Criteria crit = sess.createCriteria(Employee.class);
所以现在我想弄清楚是否有可能不这样做:
List<String> employeeIds;
Criteria crit = sess.createCriteria(Employee.class);
crit.add(Restrictions.in("id", employeeIds));
crit.createAlias("car", "car");
return crit.list();
要做到这一点:
List<Employee> employees;
Criteria crit = sess.createCriteria(Employee.class);
crit.add(Restrictions.in("", employees)); //Doesn't work, but I think you get what I'm trying to do, query by objects themselves
crit.createAlias("car", "car");
return crit.list();
我正在尝试通过员工对象列表限制Employees的查询。后来我与另一个表连接,所以限制我正在为其创建条件的类的特定实例列表是有用的。
这与此示例SQL
相同SELECT * FROM employees
JOIN cars
ON cars.ownerName like employees.name
WHERE employees.id in
(<list of employee ids goes here>);
答案 0 :(得分:2)
我唯一的想法就是你要做什么才能真正获得你要检查的id列表。 Restrictions.in意味着您指定了一个属性,因此“”将不起作用。即使这比您发布的代码多得多,我认为这是解决您发布的问题的方法:
List<Employee> employees; // This has to be the list o employees
List<Integer> ids = new ArrayList();
Iterator it = employees.iterator();
while(it.hasNext()){
ids.add(((Employee)it.next()).getId());
}
Criteria crit = sess.createCriteria(Employee.class);
crit.add(Restrictions.in("id", ids));
crit.add(Restriction.eq("car","car"));
我希望这会有所帮助。
答案 1 :(得分:0)
你试过这个吗?
Criteria crit = sess.createCriteria(Employee.class, "employee");
crit.add(Restrictions.in("employee", employees));