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.
Criteria criteria = session.createCriteria(Customer.class)
.add(Restrictions.eq("username",
"user").add(Restrictions.eq("password","12345"));
Please note that, I do consider entity name as Customer
for tbl_customer
and you created session correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.