1 package org.hibernate.criterion; 3 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import org.hibernate.type.Type; 9 import org.hibernate.util.ArrayHelper; 10 11 20 public class Restrictions { 21 22 Restrictions() { 23 } 25 26 32 public static Criterion idEq(Object value) { 33 return new IdentifierEqExpression(value); 34 } 35 41 public static SimpleExpression eq(String propertyName, Object value) { 42 return new SimpleExpression(propertyName, value, "="); 43 } 44 50 public static SimpleExpression ne(String propertyName, Object value) { 51 return new SimpleExpression(propertyName, value, "<>"); 52 } 53 59 public static SimpleExpression like(String propertyName, Object value) { 60 return new SimpleExpression(propertyName, value, " like "); 61 } 62 68 public static SimpleExpression like(String propertyName, String value, MatchMode matchMode) { 69 return new SimpleExpression(propertyName, matchMode.toMatchString(value), " like " ); 70 } 71 79 public static Criterion ilike(String propertyName, String value, MatchMode matchMode) { 80 return new IlikeExpression(propertyName, value, matchMode); 81 } 82 90 public static Criterion ilike(String propertyName, Object value) { 91 return new IlikeExpression(propertyName, value); 92 } 93 99 public static SimpleExpression gt(String propertyName, Object value) { 100 return new SimpleExpression(propertyName, value, ">"); 101 } 102 108 public static SimpleExpression lt(String propertyName, Object value) { 109 return new SimpleExpression(propertyName, value, "<"); 110 } 111 117 public static SimpleExpression le(String propertyName, Object value) { 118 return new SimpleExpression(propertyName, value, "<="); 119 } 120 126 public static SimpleExpression ge(String propertyName, Object value) { 127 return new SimpleExpression(propertyName, value, ">="); 128 } 129 136 public static Criterion between(String propertyName, Object lo, Object hi) { 137 return new BetweenExpression(propertyName, lo, hi); 138 } 139 145 public static Criterion in(String propertyName, Object [] values) { 146 return new InExpression(propertyName, values); 147 } 148 154 public static Criterion in(String propertyName, Collection values) { 155 return new InExpression( propertyName, values.toArray() ); 156 } 157 161 public static Criterion isNull(String propertyName) { 162 return new NullExpression(propertyName); 163 } 164 167 public static PropertyExpression eqProperty(String propertyName, String otherPropertyName) { 168 return new PropertyExpression(propertyName, otherPropertyName, "="); 169 } 170 173 public static PropertyExpression neProperty(String propertyName, String otherPropertyName) { 174 return new PropertyExpression(propertyName, otherPropertyName, "<>"); 175 } 176 179 public static PropertyExpression ltProperty(String propertyName, String otherPropertyName) { 180 return new PropertyExpression(propertyName, otherPropertyName, "<"); 181 } 182 185 public static PropertyExpression leProperty(String propertyName, String otherPropertyName) { 186 return new PropertyExpression(propertyName, otherPropertyName, "<="); 187 } 188 191 public static PropertyExpression gtProperty(String propertyName, String otherPropertyName) { 192 return new PropertyExpression(propertyName, otherPropertyName, ">"); 193 } 194 197 public static PropertyExpression geProperty(String propertyName, String otherPropertyName) { 198 return new PropertyExpression(propertyName, otherPropertyName, ">="); 199 } 200 204 public static Criterion isNotNull(String propertyName) { 205 return new NotNullExpression(propertyName); 206 } 207 214 public static LogicalExpression and(Criterion lhs, Criterion rhs) { 215 return new LogicalExpression(lhs, rhs, "and"); 216 } 217 224 public static LogicalExpression or(Criterion lhs, Criterion rhs) { 225 return new LogicalExpression(lhs, rhs, "or"); 226 } 227 233 public static Criterion not(Criterion expression) { 234 return new NotExpression(expression); 235 } 236 246 public static Criterion sqlRestriction(String sql, Object [] values, Type[] types) { 247 return new SQLCriterion(sql, values, types); 248 } 249 259 public static Criterion sqlRestriction(String sql, Object value, Type type) { 260 return new SQLCriterion(sql, new Object [] { value }, new Type[] { type } ); 261 } 262 269 public static Criterion sqlRestriction(String sql) { 270 return new SQLCriterion(sql, ArrayHelper.EMPTY_OBJECT_ARRAY, ArrayHelper.EMPTY_TYPE_ARRAY); 271 } 272 273 278 public static Conjunction conjunction() { 279 return new Conjunction(); 280 } 281 282 287 public static Disjunction disjunction() { 288 return new Disjunction(); 289 } 290 291 298 public static Criterion allEq(Map propertyNameValues) { 299 Conjunction conj = conjunction(); 300 Iterator iter = propertyNameValues.entrySet().iterator(); 301 while ( iter.hasNext() ) { 302 Map.Entry me = (Map.Entry ) iter.next(); 303 conj.add( eq( (String ) me.getKey(), me.getValue() ) ); 304 } 305 return conj; 306 } 307 308 311 public static Criterion isEmpty(String propertyName) { 312 return new EmptyExpression(propertyName); 313 } 314 315 318 public static Criterion isNotEmpty(String propertyName) { 319 return new NotEmptyExpression(propertyName); 320 } 321 322 325 public static Criterion sizeEq(String propertyName, int size) { 326 return new SizeExpression(propertyName, size); 327 } 328 329 public static NaturalIdentifier naturalId() { 330 return new NaturalIdentifier(); 331 } 332 333 } 334 | Popular Tags |