Example queries by R4R Team

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();

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!