Hibernate Restriction GeProperty Function
In this section, you will learn about Hibernate Restrictions.geProperty() function with running example, code and its output. Here, we will see the hibernate Restrictions.geProperty() method uses with running example.
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).
Restriction.geProperty("field_name1","field_name2"): Hibernate Restriction.geProperty() function takes two parameters, both are fields name. Which you want to matched the values. This function gives you all records which is greatter and equal to second field.
Now, We will see the running example of Restrictions.geProperty() or geProperty() function. The following example match the field_name1 = "empSal" and field_name2 = "empExpences". It gives you all results which matched all values. Which is greatter and equal to empSal and empExpences.
package developerhelpway.hibernate.criteria.restriction;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import developerhelpway.hibernate.Employee;
public class RestrictionGePropertyExample {
/**
* @param args
*/
public static void main(String[] args) {
Session sess = null;
try{
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
sess = sf.openSession();
Transaction tr = sess.beginTransaction();
Criteria criteria = sess.createCriteria(
Employee.class);
criteria.add(Restrictions.geProperty
("empSal", "empExpences"));
List<Employee> employees = criteria.list();
System.out.println("Get All Employees which
salary is gretter and equal to expences:");
for(Employee emp: employees){
System.out.println(
"Id: " + emp.getEmpId()
+ ", EmpName: " + emp.getEmpName()
+ ", EmpSal: " + emp.getEmpSal()
+ ", EmpExpences: " + (emp.getEmpExpences()
!= null ? emp.getEmpExpences() : 0.0)
);
}
tr.commit();
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(sess != null){
sess.close();
}
}
}
}
|
Download Restriction geProperty function Example
After running this example you will see the following output and SQL query which is generated by hibernate:
Hibernate: select this_.emp_id as emp1_0_0_, this_.emp_name as emp2_0_0_, this_.emp_sal as emp3_0_0_, this_.emp_expences as emp4_0_0_ from employee this_ where this_.emp_sal>=this_.emp_expences
OutPut:
