user2963481
user2963481

Reputation: 2051

How to use criteria API for hibernate?

I am working on Hibernate I have Emp table but I want to fetch the data like this

String query = "Select * from emp where ename='bhanu' or ename='hari' or ename='balu'";

How can I do this by using Hibernate Criteria API?

Upvotes: 0

Views: 112

Answers (1)

Hrishikesh
Hrishikesh

Reputation: 2053

Why do you need an or condition here? Can you not do an IN query instead? Also, i would suggest you look up the hibernate documentation for the criteria query API. It is pretty basic and will help you understand and structure your DB layer.

Here is how you do it using the Criteria API.

List employees = session.createCriteria(Emp.class)
    .add(Restrictions.or(
Restrictions.eq("ename", "bhanu") ,Restrictions.eq("ename", "hari") ,Restrictions.eq("ename", "balu") 
    ) )
    .list();

Upvotes: 2

Related Questions