如何在 hibernate 中编写以下 JDBC 查询?
Select * from Date_Table where
(START_DT>='2011-05-01 00:00:00.000000' and END_DT<='2011-05-02 00:00:00.000000' )
or
( START_DT<'2011-05-01 00:00:00.000000' and END_DT>='2011-05-01 00:00:00.000000' )
or
( START_DT<'2011-05-02 00:00:00.000000' and END_DT>'2011-05-01 00:00:00.000000' )
order by
START_DT asc
这是正确的方法吗?
Criteria criteria=session.createCriteria(Hours.class);
Criterion one=Restrictions.ge("startDate", startDate);
Criterion two=Restrictions.le("endDate", endDate);
LogicalExpression andOne=Restrictions.and(one, two);
Criterion one_a=Restrictions.lt("startDate", startDate);
Criterion two_a=Restrictions.gt("startDate", startDate);
LogicalExpression andOne_a=Restrictions.and(one_a, two_a);
Criterion one_b=Restrictions.lt("endDate", endDate);
Criterion two_b=Restrictions.gt("endDate", endDate);
LogicalExpression andOne_b=Restrictions.and(one_b, two_b);
LogicalExpression or =Restrictions.or(andOne,andOne_a);
LogicalExpression or1 =Restrictions.or(or,andOne_b);
criteria.add(or1);
criteria.addOrder(Order.desc("startDate"));
最佳答案
你测试过吗?有效吗?
我还没有测试过,但我想说你走在正确的道路上。如果您遇到麻烦 让它发挥作用,然后尝试打破一切。仅使用第一个 AND 子句运行查询。当该方法有效时,尝试实现第二个 AND 子句(这要求您实现第一个 OR 条件)。
如果您无法使其正常工作,我建议您返回 SO 并提出更精确的问题。祝你好运!
关于java - 使用条件对应的 hibernate 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6502654/