Question

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?

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top