what is org.hibernate.criterion.Example class?

2.1k views Asked by At

i'm beginner in hibernate ORM. i found a class Example(org.hibernate.criterion.Example) . I've searched a lot about the same class but i couldn't find what its real use or any good example. Hibernate documentation is not providing me enough information for understanding this class.

it would be great if any one can provide me a best example and explain what are the uses of the Example class

Thanks in Advance

1

There are 1 answers

0
Grim On BEST ANSWER

The example builds the where-block.

To find every user of age 18 it would be:

User u = User();
u.setAge(18);
Collection<User> users = session.createCriteria(User.class).add(Example.create(u)).list();
// SELECT * FROM User WHERE age=18

In the collection users are only users that are 18 in age.

Hint: The example introspects every field that is not null. This means that if the primary-key is an int it is 0 by default and added into the where-block to WHERE age=18 AND id=0. Make the pk an Integer to use it properly.