除了less代码之外,使用Hibernate Criteria API构建IN子句的以下两种方法之间的区别是什么?是否存在性能问题?我遗漏的检索中有没有什么逻辑?就返回的行数而言,它们的性能似乎是相同的。
Disjunction disj = Restrictions.disjunction();
for (String value : stringArray) {
disj.add(Restrictions.eq("code", value));
}
where.add(disj);
VS.
Restrictions.in("code", stringArray);
我问的原因是因为我正在重构遗留代码,因为前者存在,但我期待的是后者。如果它们都是一样的,我就不去管遗留代码了。
发布于 2011-05-06 04:09:57
Hibernate Disjunction用于
Group expressions together in a single disjunction
这意味着,如果您必须与值X、Y或Z conditionally,进行比较,则可以遍历并应用selective disjunction
所以在理想情况下,Restrictions.in和Restrictions.Disjunction做同样的事情,我更喜欢前者。
发布于 2014-04-16 04:11:44
Restrictions.Disjunction给了我们明确的控制,例如,它允许like运算符,而as in运算符不允许。
例如:
criteria.add(Restrictions.disjunction()
.add(Restrictions.eq("bill.stateCd", null))
.add(Restrictions.eq("bill.stateCd", ""))
.add(Restrictions.ilike("bill.stateCd","%"+stateCd+"%")));
不能用in实现
criteria.add(Restrictions.in("bill.stateCd", Arrays.asList(null,"", "%"+stateCd+"%")));
发布于 2014-06-06 03:15:09
在给定的代码中,当stringArray
没有任何元素时,这两种方法的行为非常不同。
对零表达式使用析取将生成带有1=1
或等效表达式的有效SQL查询。
Restrictions.in
会导致没有值的IN运算符,这在语法上通常是不正确的(也许某些SQL方言可以处理它)。
https://stackoverflow.com/questions/5902616
复制相似问题