2

因此,如果我要对员工进行限制,我会说

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();

我试图通过员工对象列表限制对员工的查询。稍后我与另一个表进行连接,因此通过我正在为其创建条件的类的特定实例列表进行限制是很有用的。

这将与此示例 SQL 相同

SELECT * FROM employees
    JOIN cars
        ON cars.ownerName like employees.name
    WHERE employees.id in
        (<list of employee ids goes here>);
4

2 回答 2

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"));

我希望这有帮助。

于 2013-12-10T22:02:09.787 回答
0

你试过这个吗?

     Criteria crit = sess.createCriteria(Employee.class, "employee");
     crit.add(Restrictions.in("employee", employees));
于 2012-08-05T17:21:43.077 回答