tags:

views:

33

answers:

2

Hi

I want to execute a query using hibernate where the requirment is like

select * from user where regionname=''

that is select all the users from user where region name is some data How to write this in hibernate The below code is giving result appropraitely

Criteria crit= HibernateUtil.getSession().createCriteria(User.class);
        crit.add(Restrictions.eq("regionName", regionName));
+1  A: 
String hql = "SELECT u FROM User u WHERE regionName=:regionName";
Query q = session.createQuery(hql);
q.setParameter("regionName", regionName);
List result = q.list();
Bozho
+1  A: 

Well as you alaready said you can either use the Criteria API or create a HQL query:

// Criteria
List<User> users = HibernateUtil.getSession().createCriteria(User.class);
        crit.add(Restrictions.eq("regionName", regionName)).list();

// HQL
String query = "SELECT FROM User WHERE regionName = :region";
List<User> users = HibernateUtil.getSession().createQuery(query).setString("region", regionName).list();
Daff
I am doing the same
sarah
I just wanted to show the alternative to the Criteria you are using in your example with a HQL query. If you are doing the same, what is your question then?
Daff
i am not getting the complete resultset with it
sarah
I don't see any problems with either the criteria or HQL. Have you tried to issue a SQL query directly and see if it returns what you want?
Daff