Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Java Hibernate Cookbook

You're reading from  Java Hibernate Cookbook

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781784391904
Pages 250 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Java Hibernate Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Setting Up Hibernate 2. Understanding the Fundamentals 3. Basic Annotations 4. Working with Collections 5. Working with Associations 6. Querying 7. Advanced Concepts 8. Integration with Other Frameworks Index

Restricting the results using a criteria


Let's take a look at how to add restrictions, which are equal to the WHERE clause in SQL.

How to do it…

Let's consider that we have four records in the employee table, as shown in the following tables:

This is the Employee table:

department

salary

firstName

id

1

50000

Yogesh

1

1

35000

Aarush

2

3

30000

Varsha

2

2

75000

Vishal

4

This is the Department table:

deptName

id

development

1

R&D

2

UI/UX

3

Now, the scenario is that we want to get only those employees whose salary is greater than 35000.

The equivalent SQL query to select the above employees is as follows:

SELECT * FROM employee WHERE salary > 35000;

Now, let's look at how to do the same using hibernate.

Code

Enter the following code to create a criteria for employee:

Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.gt("salary", 35000));
List<Employee> employees = criteria.list();
for...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}