Let me consider one simple sql query
select username from tbl_customer where username="user" and password="12345";
How can i write this query in hibernate using criteria
Thanks. Hoping for positive response..
first you have to create the Criteria object, then you need to pass the where clause condition to the criteria object as well you have to set the projection property for selecting particular column data.
By seeing your SQL query,
select username from tbl_customer where username="user" and password="12345";
I believe you want to fetch the particular column username
from the table tbl_customer
. This can be done using Projections
. You have to set the projection property for which column data you want.
Criteria criteria = session.createCriteria(MyClass.class)
criteria.setProjection(Projections.property("username"));
criteria.add(Restrictions.and(Restrictions.eq("username", user),Restrictions.eq("password", 12345))
);
List<String> userNames = criteria.list();
This will return only the username column data from that table.