0

I am using spring and hibernate together.

Can any body suggest me how can i execute a simple query?

For example, I want to execute "select count(*) from USER_DETAILS";

Thanks,
Narendra

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
Narendra
  • 5,635
  • 10
  • 42
  • 54

2 Answers2

1

Hiii... Use criteria and projection together. Projection

Criteria crit = session.createCriteria(USER_DETAILS.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.countDistinct("Id"));
crit.setProjection(projList);

crit.list will give you count. This is simple hibernate code you can figure out spring + hibernat with this example.

Ashfak Balooch
  • 1,879
  • 4
  • 18
  • 30
0

Hibernate is a Object-Relational Mapping tool. You first map a User object to USER_DETAILS table and then you write HQL (Hibernate Query Language) against the mapped User object (not USER_DETAILS table). For e.g you can write the query you have posted as below using HQL.

select count(user) from User user;

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327