The class org.hibernate.criterion.Example allows to construct a query criterion from a given instance.
As we shown in the below query:
Student student = new Student();
student.setSex('M');
student.setColor(Color.BLACK);
List results = session.createCriteria(Student.class)
.add( Example.create(student) )
.list();
Version properties, identifiers and associations are ignored. By default, null valued properties are excluded. Here we can adjust how the Example is applied.
Example example = Example.create(student)
.excludeZeroes() //exclude zero valued properties
.excludeProperty("color") //exclude the property named "color"
.ignoreCase() //perform case insensitive string comparisons
.enableLike(); //use like for string comparisons
List results = session.createCriteria(Student.class)
.add(example)
.list();
We can even use examples to place criteria upon associated objects Like we give below:
List results = session.createCriteria(Student.class)
.add( Example.create(student) )
.createCriteria("mate")
.add( Example.create( cat.getMate() ) )
.list();