2013-03-25 20 views
0

我的SQL應該是這樣的:SQL Hibernate和或限制

select cell1 from table 
where cell2 = 1 
and (cell3 = '' or cell3 is null) 

但如何做休眠的 「和(X或Y)」 restricion?

+0

可能duplicat http://stackoverflow.com/questions/57484/how-do-you-or-criteria-together-when-using-a-criteria-query-with-hibernate – tostao 2013-03-25 08:22:56

+0

@tostao:不是。您鏈接的問題是指Hibernate的Criteria OR機制,而這個問題通常指的是Hibernate OR(例如HQL) – 2013-03-25 08:57:12

回答

0

什麼LogicalExpression

嘗試此條件:

Criteria cr = session.createCriteria(table.class); 

// To get records matching with AND condistions 
LogicalExpression andExp = Restrictions.and(cell2, cell3); 
cr.add(andExp); 

對於OR條件使用該

// To get records matching with OR condistions 
    LogicalExpression orExp = Restrictions.or(cell2, cell3); 
    cr.add(orExp); 

List results = cr.list();