Introduction To Hibernate Restriction
In this section, you will learn about Hibernate restriction and restriction functions with running example and its output. First of all we will read hibernate restriction. Here we will also discussed about the SQL FROM clause.
Restriction: Hibernate restriction is the more importaint feature in hibernate. As we know that SQL "WHERE" clause that restricts the results for user. Similiarly, hibernate restriction helps us to restrict the result according to user requirement. Hibernate Restriction is not only applied on SELECT statement. We applied on also UPDATE and DELETE.
Now, We will see or learn the various types of hibernate restriction functions with running examples and how to use the hibernate restrictions in hibernate to remove SQL WHERE restriction clause.
Restriction Functions: There are following types of restriction function:
1. Restrictions.allEq(): Hibernate restriction allEq() method used with Map type object. That means it matched with multiple values of records.
2. Restrictions.eq(): Hibernate restriction eq() method used with Restrictions. You can add your restriction with eq() function. eq() is matched with equal to given field name and its value
3. Restrictions.eqProperty(): Hibernate restriction eqProperty() method is used with Restrictions. The eqProperty() function matched with two fields (table's column name) values will be same or equal.
4. Restrictions.ge(): Hibernate Restrictions.ge() method is used with Restrictions. The ge() function gives you the result which is greatter and equal to added fieldName and its value. ge() method return list of result which is greatter and equal to given conditions.
5. Restrictions.geProperty(): Hibernate Restrictions.geProperty() method is used with Restrictions. The Restriction.geProperty() function matched both two fields (table's column name) values. This method gives you all results which will be greatter and equal second filed (column).
6. Restrictions.gt(): Hibernate restriction gt() method is used with Restrictions. The hibernate Restriction.gt() function matched with given value to specified field (table's column name). It gives you all results which matched with given value to its field.
7. Restrictions.gtProperty(): Hibernate restrictions.gtProperty() method is used with Restrictions. The hibernate Restrictions.gtProperty() function matched with two fields (table's column name) values. It gives you all results which is greatter than other column value.
8. Restrictions.le(): Hibernate restriction le() method is used with hibernate Restrictions. The hibernate Restrictions.le() function matched with given value which are less and equal to given value.
9. Restrictions.leProperty(): Hibernate criteria restriction leProperty() method is used with Restrictions. The hibernate Restrictions.leProperty() function matched values to specified fields (table's column name) to each other. It gives you all results which is matched the value to empSal is less or equal to empExpences.
10. Restrictions.like(): Hibernate criteria restriction like() method is used with Restrictions. Hibernate like() function similar to hibernate like MatchMode.EXACT. The hibernate Restrictions.like() function matched values to specified fields (table's column name) and given like query string.
11. Restrictions.like("fieldName", "like_query_string", MatchMode.START): Hibernate Restrictions.like() function takes three parameters. First is fieldName, second is like_query_string and other is like query MatchMode. It gives you all records which are matched with given like_query_string from begning or starting from.
12. Restrictions.like("field_name","like_query_string", MatchMode.END): Hibernate Restrictions.like() function with MatchMode.END takes three parameters. First is fieldName, second is like_query_string and other one is MatchMode.End. It gives you all records which empName end with given like_query_string.
13. Restrictions.like("field_name", "like_query_string", MatchMode.EXACT): Hibernate Restrictions.like() function takes three parameters:
a. field_name: Which you want to match like_query_string.
b. like_query_string: This is user matching string which you want to match.
c. MatchMode.EXACT: Like function match the query string according to MatchMode. Here we will see about the MatchMode.EXACT. MatchMode EXACT matches the value exactally to the like_query_string.
14. Restrictions.like("field_name","like_query_string", MatchMode.ANYWHERE): Hibernate Restrictions.like() function takes tree parameters. First is fieldName, second is like_query_string and other one is MatchMode.ANYWHERE. It gives you all records which having "s" string any where in empName.
15. Restictions.ne("field_name", value):Hibernate Restrictions.ne() function takes two parameters. First is filedName as table's column name and other one is value which you want to match. It gives you all records which empSal is not equal to given value.
16. Restrictions.neProperty("field_name","field_name"): Hibernate Restrictions.neProperty() function takes two parameters as table's column names (property name). It gives you all records which first filed is not qual to second filed
17. Restrictions.not(Criteria expression): Hibernate Restrictions.not() function takes a Criteria (expression) object. Under this, we give a Restions.eq("empName", value). Put the criteria to match equal value. And finally add criteria not operation. It gives you all records which not matched to given expression.
18. Restrictions.or(Criterion lhs, Criterion rhs): Hibernate Restrictions.or() function takes two parameters as Criterion objects. Each Criterion object takes hibernate restions(conditions). It gives you all records which Criterion lhs is true or Criterion rhs is true. In any one is matched then it will show in your results.
19. isNull(): Hibernate isNull() method is used with hibernate Restrictions. The hibernate Restrictions.isNull() function gives you all values which having null value for given property(table's column).
20. Restrictions.isNotNull("field_name"): Hibernate isNotNull() method is used with hibernate Restrictions. The hibernate Restrictions.isNotNull() function takes a parameter as column name or property. It gives you all records which value is not null.
21. Restrictions.in("field_name",valiues): Hibernate in() function is used with hibernate restrictions. Hibernate Restrictions.in() function takes two parameters, one is property name or table's column name and other are list of values. It matches with gives column name and values which is list type. Results which are mached with given list of values for given column.
22. Restrictions.idEq(value): Hibernate idEq() function is used with hibernate restrictions. Hibernate Restrictions.idEq() function takes a parameter as table's column name. It gives you a result with id is equal to given value.
23. Restriction.between("field_name",min_value, max_value): Hibernate between used with hibernate restrictions. Hibernate between() function takes three parameters, one is field name and other two parameters are values. This function gives you all results which is between the values of given field.
24. Restrictions.and(criteria, criteria): Hibernate and() method is used with hibernate restrictions. Hibernate and() function takes two parameters as hibernate restrictions, It gives you all records with matched the given criteria.